You need to know the new features of C version 6.0 in VS 2015

  • 2021-07-24 10:44:22
  • OfStack

This article lists several new functions that I feel are useful for your reference. The specific contents are as follows
Note: These new features can only be used in VS 2015 and later, and cannot be used in lower versions such as VS 2013 and VS 2010. Of course, if you don't like these new features, you can still continue to use the original usage (so it is a new grammar sugar).
1. Improvement of automatic attribute initialization (useful)
Original usage (cannot be initialized at the same time when declared), for example:


 class MyClass
{
  public int Age { get; set; }
  public string Name { get; set; }
  public MyClass()
  {
    Age = 20;
    Name = " Zhang 3";
  }
} 

New usage (it can be initialized at the same time when declaring, which is more convenient), such as:


 class MyClass
{
  public int Age { get; set; } = 20;
  public string Name { get; set; } = " Zhang 3";
} 

2. Improvement of String. Format (useful)
Original usage: implemented with string. Format (…), for example:


class MyClass
{
  public void MyMethod()
  {
    string name = " Zhang 3";
    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("{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: Use the "$" prefix (variables are written directly in curly braces, and with intelligent prompts, which is more convenient), such as:


class MyClass
{
  public void MyMethod()
  {
    string name = " Zhang 3";
    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. Initialization of dictionary
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 write initialized values directly, 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 declare a reference to a static class with static
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 the expression is more complex and the code is simpler):


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

5. nameof expression
Suppose you have the following classes in your WPF application:


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

And assume the following XAML code:
< StackPanel >

< TextBlock Name="txt1"/ >

...

< /StackPanel >
The original usage in the code-behind class:
txt1.SetBinding(TextBlock.TextProperty, "MyText");
The current usage (because there is an intelligent prompt for error checking, it is more convenient to use):
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 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, await could not be used in catch or finally, but now it can:


 class MyClass
{
  public int Age { get; set; } = 20;
  public string Name { get; set; } = " Zhang 3";
} 
0

8. Others
C # 6.0 also has a few new features, for beginners to use is not too much, so here will not be introduced.
Once again, if you don't like the new feature, you can still use the original usage.


Related articles: