SoFunction
Updated on 2025-04-06

New features of C# version 6.0 in VS2015 You need to know

This article lists several new features that I feel are more useful for your reference. The specific content is as follows
Notice:These new features can only be used in VS2015 and higher versions, and cannot be used in lower versions such as VS2013 and VS2010. Of course, if you don't like these new features, you can still continue to use the original usage (so it's the new syntactic sugar).
1. Improvements in 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 San";
  }
} 

New usage (can be initialized at the same time when declared, which is more convenient), for example:

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

2. Improvements (useful)
Original usage: implement with (…), for example:

class MyClass
{
  public void MyMethod()
  {
    string name = "Zhang San";
    int age = 20;
    string s1 = ("{0},{1}", name, age);
    string s2 = ("Name={0},age={1}", name, age);
    string s3 = ("{0,15},{1:d3}", name, age);
    string s4 = ("{0,15},{1,10:d3}", name, age);
    ("{0},{1},{2},{3}", s1, s2, s3 ,s4);
    string s5 = ("{0:yyyy-MM-dd}", );
  }
} 

New usage: Implemented with the "$" prefix (the variable is written directly into braces, and it has smart prompts, which is more convenient), for example:

class MyClass
{
  public void MyMethod()
  {
    string name = "Zhang San";
    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}";
    ($"{s1},{s2},{s3},{s4}");
    string s5 = $"{:yyyy-MM-dd}";
  }
} 

3. Initialization of dictionary
Original usage:

class MyClass
{
  public void MyMethod()
  {
    Dictionary<string, int> student = new Dictionary<string, int>();
    ("a1", 15);
    ("a2", 14);
    ("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 references to static classes using static class
Original usage:

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

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

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

5. Nameof expression
Assume that there are the following classes in the WPF application:

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

And assume that the following XAML code is:
 <StackPanel>
 
<TextBlock Name="txt1"/>
 
……
 
</StackPanel>
Original usage in code-behind classes:
 (, "MyText");
The current usage (because there is a smart error checking prompt, it is more convenient to use):
 (, nameof());
6. Null-conditional expression
(it works)

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

7. Use await in try-catch-finally
In asynchronous programming, await was not available in catch or finally, now it is OK:

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

8. Others
C# 6.0 has some new features, which are not used too much for beginners, so I won’t introduce them here.
Let me explain again that if you don’t like new features, you can still continue to use the original usage.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.