New features of C # version 6.0 in VS2015 you need to know the practical skills

Source: Internet
Author: User

This article lists several new features that are useful for your personal feeling, for your reference.
Note: These new features can only be used for VS2015 and later versions and cannot be used in VS2013, VS2010, and other lower versions. Of course, if you don't like the new features, you can still continue using the original usage (so it's the new syntax candy).
1. Automatic Property initialization improvements (useful)
The original usage (cannot be initialized at the same time when declaring), for example:

 Class MyClass
{public
  int age {get; set;}
  public string Name {get; set;}
  Public MyClass ()
  {age
    =;
    Name = "John";
  }
} 

New usage (the declaration can be initialized at the same time, more convenient), for example:

 Class MyClass
{public
  int age {get; set;} =;
  public string Name {get; set;} = "John";
} 

2, String.Format Improvement (useful)
Original usage: use a string. Format (...) Implementation, for example:

Class MyClass
{public
  void MyMethod ()
  {
    string name = ' John ';
    int age =;
    string S1 = string. Format ("{0},{1}", name, age);
    String s2 = string. Format ("Name ={0}, age ={1}", name, aged);
    String s3 = String. Format ("{0,15},{1:d3}", name, age);
    String S4 = String. Format ("{0,15},{1,10: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: Implement with "$" prefix (variables written directly into curly braces, and with smart hints, more convenient), for example:

Class MyClass
{public
  void MyMethod ()
  {
    string name = ' John ';
    int age =;
    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, the initialization of the dictionary
The original usage:

Class MyClass
{public
  void MyMethod ()
  {
    dictionary<string, int> student = new dictionary< String, int> ();
    Student. ADD ("A1");
    Student. ADD ("A2",);
    Student. ADD ("A3",);
  }
 

New usage (you can write initialized values directly, more convenient):

Class MyClass
{public
  void MyMethod ()
  {
    dictionary<string, int> student = new Dictionary <string, int> ()
    {
      ["a1"] =,
      ["A2"] =
      N, ["A3"] =
    };
} 

4. You can declare static class references with static
The 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 more complex, simpler code):

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 following classes are in the WPF application:

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

And assume that you have the following XAML code:
<StackPanel>

<textblock name= "Txt1"/>

......

</StackPanel>
original usage in the code-behind class:
Txt1. SetBinding (Textblock.textproperty, "MyText");
Current usage (because of the error checking smart tip, it's easier to use):
Txt1. SetBinding (Textblock.textproperty, nameof (Myclass.mytext));
6, null-condition expression
(useful)

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

7. Use await in try-catch-finally
in asynchronous programming, you can't use await in catch or finally, and now you can:

Async void SomeMethod ()
{
  try
  {
    //...etc ...
  }
  catch (Exception x)
  {
    var diagnosticdata = await generatediagnosticsasync (x);
    Logger.log (Diagnosticdata);
  }
  Finally
  {
    await someobject.finalizeasync ();
  }
} 

8. Other
C # 6.0 also has some new features, not too much for beginners, so this is no longer introduced.
Again, if you don't like the new features, you can still use the original usage.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.