C# 6.0 Features

In the latest C# 6.0 there are so many changes and improvements made. In this article I will list a few of the C# 6.0 features introduced. The features are:

  1. Auto-property initializers
  2. nameof expressions
  3. Null conditional operators
  4. Exception filters
  5. Await in catch and finally blocks
  6. Using Static

Auto-property Initializers

As a developer we encounter auto-properties every day. With the introduction of this new feature of auto-property initializer, the overhead of invoking a setter and initializing the property in the constructor is removed. This new feature of auto-property initializer will initialize the value to the property in the declaration only. For example:

  1. public class Employee  
  2. {  
  3.    public string FirstName { getset; } = "John";  
  4.    public string LastName { getset; } = "Snow";  
  5. }  
This feature can also be used with read-only properties (properties with getters only) as in the following:
  1. public string FirstName { get; } = "John";  
nameof Expressions

There are various scenarios we encounter in which we must provide some string literals that name some program element. For example, when throwing an ArgumentNullException, we want to name the invalid parameter, or when raising a PropertyChanged event we want to name the property that is changed, and so on. The string literals are error-prone and can be misspelled that cannot be validated at compile time. By using nameof expressions, the compiler checks the referred name.
  1. if (obj == nullthrow new ArgumentNullException(nameof(obj));  
Null-conditional operator

The null-conditional operator checks for a null value before invoking any member. This will let you invoke or access the members only if the variable is not null. For example:
  1. int? _count = employees?.Count(); // returns null if employees are null else count   
The null conditional operator can be used with the null-coalescing operator (??) to assign some default value if the returned value is null. For example:
  1. int _count = employees?.Count() ?? 0; //returns 0 if the employees are null  
Exceptions Filters

This feature was already present in VB and F# but newly introduced in C#. Using exception filters one can specify an if condition in the catch statement. In case the if condition evaluates to true only then the catch will be executed. For example:
  1. try  
  2. {  
  3.   
  4. }  
  5. catch(exception ex) if(ex.Message == “Test”)  
  6. {  
  7.    // this will execute only if the if statement evaluates to true  
  8. }  
Await in catch and finally blocks

With C# 6.0 a new feature is added to exception handling. Now we can use an await in catch and finally statements. For example:
  1. try  
  2. {  
  3.   
  4. }  
  5. catch(Exception ex)  
  6. {  
  7.    await LogAsync(ex);  
  8. }  
Using Static

In C# 6.0, a different kind of using clause is introduced by which we can access static members directly. For example:
  1. using System.Console;  
  2. namespace MyConsoleApp  
  3. {  
  4.    class Sample  
  5.    {  
  6.       static void Main(string[] args)  
  7.       {   
  8.          WriteLine(“Hello!!!”);  
  9.       }  
  10.    }  
  11. }  

 

Up Next
    Ebook Download
    View all
    Learn
    View all