Some New Features in C# 6.0

Static Using Syntax

In previous versions of C#, we would need to add the proper using statement, such as System, then we could write the following line of code:
  1. Console.WriteLine("Hello Abrar!");  
With C# 6, you can now add the using statement and reference the WriteLine method by itself as in the following:
  1. using System.Console;  
  2. namespace Console1  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args  
  7.         {  
  8.             //Sample One    
  9.             WriteLine("Hello Abrar!");  
  10.         }  
  11.         }  
  12.     }  
Auto-Property Initializers

In the past, we may have created our properties with a get and set and then initialized our constructor with the value that we wanted as shown below.
  1. public class Customer  
  2. {  
  3.     public Customer()  
  4.     {  
  5.         customerID = Guid.NewGuid();  
  6.     }  
  7.     public Guid customerID   
  8.     {  
  9.         get;  
  10.         set;  
  11.     }  
  12. }  
We can now reduce this code block to one line as shown below. No longer do we need to create a setter or constructor.
  1. public class Customer  
  2. {  
  3.    public Guid customerID { getset; } = Guid.NewGuid();  
  4. }  
Dictionary Initializers

Many C# developers felt the format shown below was confusing for creating dictionaries, mainly because of the use of brackets and quotation marks for the data.
  1. Dictionary<stringstring> customerNames = new Dictionary<stringstring>()  
  2. {  
  3.    { "Abrar Ahmad"".net" },  
  4.    { "Yogesh""Java" },  
  5.    { "Ganesh""SQL" }  
  6. };  
The compiler team decided to change this and make it more readable with the following format. This new format will only save you a few keystrokes, but it is much easier to read in my opinion.
  1. public Dictionary<stringstring> customerNames = new Dictionary<stringstring>()  
  2. {  
  3.    ["Abrar Ahmad "] = ". net ",  
  4.    ["Yogesh"] = "Java",  
  5.    ["Ganesh "] = " SQL "  
  6. };  
Exception Filters and Async in a Catch and Finally Block

Exception Filters

Exception filters have been supported in Visual Basic, but are new to the C# compiler. They allow you to specify a condition for a catch block. As shown in the following sample, the last catch statement will fire.
  1. public static async void DownloadAsync()   
  2. {  
  3.     try  
  4.     {  
  5.         throw new Exception("Error");  
  6.     } catch   
  7.     {  
  8.         await Task.Delay(2000);  
  9.         WriteLine("Waiting 2 seconds");  
  10.     }   
  11.     finally   
  12.     {  
  13.         await Task.Delay(2000);  
  14.         WriteLine("Waiting 2 seconds");  
  15.     }  
  16. }  
Async in a Catch and Finally Block

I think many developers will love this feature because they often need to log exceptions to a file or database without blocking the current thread. Here is an example of how one would work:
  1. public static async void DownloadAsync()  
  2. {  
  3.     try   
  4.     {  
  5.         throw new Exception("Error");  
  6.     }   
  7.     catch   
  8.     {  
  9.         await Task.Delay(2000);  
  10.         WriteLine("Waiting 2 seconds");  
  11.     }  
  12.     finally  
  13.     {  
  14.         await Task.Delay(2000);  
  15.         WriteLine("Waiting 2 seconds");  
  16.     }  
  17. }  
Name of Expressions

How many times in your code do you retrieve the name of a member of your class? You would typically wrap the field in quotation marks as shown below.
  1. public static void DoSomething(string name)  
  2. {  
  3.    if (name != nullthrow new Exception("name");  
  4. }  
The main problem with this is that it forces you to put strings in your code to represent the name of a member. In C# 6.0, there is a new operator called nameof that will allow us to refactor our code to remove these string literals. The sample shown below will show how we could refactor that same method.
  1. public static void DoSomething(string name)  
  2. {  
  3.    if (name != nullthrow new Exception(nameof(name));  
  4. }  
This results in cleaner code and safety when retrieving member names.

String Interpolation

Prior to C# 6.0, we typically concatenate two or more strings together in one of the following ways:
  1. string firstName = "Abrar";  
  2. string lastName = "Ahmad";  
  3. WriteLine("Name : " + firstName + " " + lastName);  
  4. WriteLine(string.Format("Name : {0} {1}", firstName, lastName));  
In C# 6.0, we have a cleaner format as shown in the first WriteLine call, but we can also put expressions directly in the string literal to evaluate an expression as shown below:
  1. string firstName = "Abrar";  
  2. string lastName = "Ahmad";  
  3. WriteLine("Name : \{firstName} \{lastName}");  


Similar Articles