You need to know the new features of C #6.0 in VS2015,

Source: Internet
Author: User

You need to know the new features of C #6.0 in VS2015,

This article lists several new functions that are useful to you. The specific content is as follows:
Note:These new features can only be used in VS2015 and later versions, and cannot be used in earlier versions such as VS2013 and VS2010. Of course, if you do not like these new features, you can continue to use the original usage (so it is a new syntactic sugar ).
1. Improvement on automatic attribute initialization (useful)
Original usage (initialization cannot be performed at the same time during Declaration), for example:

Class MyClass {public int Age {get; set;} public string Name {get; set;} public MyClass () {Age = 20; Name = "James ";}}

New usage (initialization can be performed at the same time during declaration, which is more convenient), for example:

Class MyClass {public int Age {get; set ;}= 20; public string Name {get; set ;}= "James ";}

2. Improved String. Format (useful)
Original usage: Use string. Format (...) For example:

Class MyClass {public void MyMethod () {string name = "Zhang San"; int age = 20; string s1 = string. format ("{0}, {1}", name, age); string s2 = string. format ("name = {0}, age = {1}", name, age); string s3 = string. format ("{0, 15}, {1: d3}", name, age); string s4 = string. format ("{}, {: d3}", name, age); Console. writeLine ("{0}, {1}, {2}, {3}", s1, s2, s3, s4); string s5 = string. format ("{0: yyyy-MM-dd}", DateTime. now );}}

New usage: implemented with the "$" prefix (variables are directly written in braces and are more convenient with smart prompts), for example:

Class MyClass {public void MyMethod () {string name = "zhangsan"; int age = 20; string s1 =$ "{name}, {age }"; string s2 = $ "name = {name}, age = {age}"; string s3 = $ "{name, 15}, {age: d3 }"; string s4 = $ "{name, 15}, {age, 10: d3}"; Console. writeLine ($ "{s1}, {s2}, {s3}, {s4}"); string s5 = $ "{DateTime. now: yyyy-MM-dd }";}}

3. dictionary Initialization
Original usage:

class MyClass{  public void MyMethod()  {    Dictionary<string, int> student = new Dictionary<string, int>();    student.Add("a1", 15);    student.Add("a2", 14);    student.Add("a3", 16);  }} 

New usage (you can directly write the initialization value, which is more convenient ):

class MyClass{  public void MyMethod()  {    Dictionary<string, int> student = new Dictionary<string, int>()    {      ["a1"] = 15,      ["a2"] = 14,      ["a3"] = 16    };  }} 

4. You can use static statements to declare static class references.
Original usage:

using System;namespace MyApp{  class Demo1New  {    public static double MyMethod(double x, double angle)    {      return Math.Sin(x) + Math.Cos(angle);    }  }} 

New usage (useful when expressions are complex and the code is more concise ):

using static System.Math;namespace MyApp{  class Demo1New  {    public static double MyMethod(double x, double angle)    {      return Sin(x) + Cos(angle);    }  }} 

5. nameof expression
Assume that the WPF application contains the following classes:

public class MyClass { public string MyText { get; set; } = "aaa"; }

Assume that the following XAML code is available:
<StackPanel>
 
<TextBlock Name = "txt1"/>
 
......
 
</StackPanel>
Original usage of the Code hiding class:
Txt1.SetBinding (TextBlock. TextProperty, "MyText ");
Current usage (it is more convenient to use it because of smart prompts for error checks ):
Txt1.SetBinding (TextBlock. TextProperty, nameof (MyClass. MyText ));
6. Null-conditional expression
(Useful)

Var ss = new string [] {"Foo", null}; var length0 = ss [0]?. Length; // The result is 3var length1 = ss [1]?. Length; // The result is nullvar lengths = ss. Select (s => s ?. Length ?? 0); // The result is [3, 0]

7. Use await in try-catch-finally
In asynchronous programming, await cannot be used in catch or finally. Now we can:

async void SomeMethod(){  try  {    //...etc...  }  catch (Exception x)  {    var diagnosticData = await GenerateDiagnosticsAsync (x);    Logger.log (diagnosticData);  }  finally  {    await someObject.FinalizeAsync();  }} 

8. Others
C #6.0 has some new features that are not used too much for beginners, so I will not introduce them here.
Again, if you do not like the new features, you can continue to use the original usage.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.