你的位置:首页 > 软件开发 > ASP.net > What's New in C# 6.0(转)

What's New in C# 6.0(转)

发布时间:2015-09-06 11:00:17
原文地址:http://www.codeproject.com/Tips/1023426/Whats-New-in-Csharp 本来想翻译一下贴出来,但是好像很多语言组织起来比较困难,读书少不会表达了。 最近一直强迫自己都一些英文资料努力提交英文阅读水平。 从c#6发布有段时间 ...

原文地址:http://www.codeproject.com/Tips/1023426/Whats-New-in-Csharp

本来想翻译一下贴出来,但是好像很多语言组织起来比较困难,读书少不会表达了。

最近一直强迫自己都一些英文资料努力提交英文阅读水平。

从c#6发布有段时间了,一个片段一个片段的看到说c#6的新特性,感觉都不是很全,总感觉老外貌似有好多时间去整理,而且整理的很详细。

 

 

C# 6.0. features walkthrough with easy to understand examples.

 

Introduction

Get a quick glance at what's new in C# 6.0. Features walkthrough with easy to understand examples.

 

What's New in C# 6.0

 

 

Static Types as using

So, we are all quite familiar with this notion of accessing static class members using the qualification first. It is not required now. Importing static using can save the day. For example, before C# 6.0, we had to accessReadKey(), WriteLine() methods of the Console class by explicitly defining the static class qualifier.

using System;namespace NewInCSharp{  class Program  {    static void Main(get='_blank'>string[] args)    {      MySelf.WhoAmI();      Console.ReadKey();    }  }  static class MySelf  {    public static void WhoAmI()    {      Console.WriteLine("I'm Fizz!");    }  }}
 

In C# 6.0, we can get rid of the qualifiers just by importing the static types in the namespace like below:

using static System.Console;using static NewInCSharp.MySelf;  /* Magic happens here */namespace NewInCSharp{  class Program  {    static void Main(string[] args)    {      WhoAmI();      ReadKey();    }  }  static class MySelf  {    public static void WhoAmI()    {      WriteLine("I'm Fizz!");    }  }}
String Interpolation

You can now forget using placeholders in strings to replace them with real values. C# 6 has a new feature called string interpolation using which you can now directly write your arguments instead of referring them with placeholders inside a string. You can also do whatever you would have done previously withString.Format() function.

Before C# 6.0
using System;using System.Collections.Generic;using static System.Console;namespace NewInCSharp{  class Program  {    private static void Main(string[] args)    {      string name = "Murphy Cooper";      string planet = "Cooper Station";      WriteLine("{0} is actually named after {1}", planet, name);      ReadLine();    }  }  }
After C# 6.0
using System;using System.Collections.Generic;using static System.Console;namespace NewInCSharp{  class Program  {    private static void Main(string[] args)    {      string name = "Murphy Cooper";      string planet = "Cooper Station";            /* Magic happens here */      WriteLine($"{planet} is actually named after {name}");      ReadLine();    }  }  }

Again, these are a few string formatting examples which I missed in the video. [Check the video link at the bottom of the article.]

string name = "Sammy Jenkins";double salary = 1000;WriteLine($"{name}'s monthly salary is {salary:C2}"); WriteLine($"Man! This {name} is kind of a {(salary >= 1000 ? "rich guy" : "poor guy")}");/*Output:   Sammy Jenkins's monthly salary is $1000.00  Man! This Sammy Jenkins is kind of a rich guy*/
Dictionary Initializers

C# 6.0 changed the way you can initialize Dictionary. Previously on C# 5, you would have to initialize theDictionary with this type of syntax, {"Key", "Value"}. Now in C# 6, you can just place the key between two curly brackets ["Key"] and then set the value of the key ["Key"] = "value"; , just like you set the value for other types of variable. This new syntax is friendlier than before.

Before C# 6

 

using System.Collections.Generic;using static System.Console;namespace NewInCSharp{  class Program  {    private static void Main(string[] args)    {      Dictionary<string, string="">       alien = new Dictionary<string, string="">()      {        {"Name", "Fizzy"},        {"Planet", "Kepler-452b"}      };      foreach (KeyValuePair<string, string=""> keyValuePair in alien)      {        WriteLine(keyValuePair.Key + ": " +         keyValuePair.Value + "\n");      }      ReadLine();    }  }  }
In C# 6.0

 

using System.Collections.Generic;using static System.Console;namespace NewInCSharp{  class Program  {    private static void Main(string[] args)    {      /* The new and friendly syntax */       Dictionary<string, string=""> alien = new Dictionary<string, string="">()      {        ["Name"] = "Fizzy",        ["Planet"] = "Kepler-452b"      };      foreach (KeyValuePair<string, string=""> keyValuePair in alien)      {        WriteLine(keyValuePair.Key + ": " + keyValuePair.Value + "\n");      }      ReadLine();    }  }  }
Auto-Property Initializers

C# 6.0 came with a new concept of initializing class properties inline rather than initializing them within the type's constructor. Another handy technique is, if you want to make the setter of a property private to block users from setting value in the property by an instance, you can just declare a getter only property. For example:

Before C# 6

 

using static System.Console;namespace NewInCSharp{  class Program  {    static void Main(string[] args)    {      Employee employee = new Employee();      WriteLine("Name: " +       employee.Name + "\nSalary: " + employee.Salary);      ReadKey();    }    public class Employee    {      public string Name { get; set; }       public decimal Salary { get; set; }      public Employee()      {        /* Initializing property through constructor */        Name = "Sammy Jenkins";        Salary = 10000;       }    }  }}
 

原标题:What's New in C# 6.0(转)

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录