;
vote buttons
22
03 Jan 2015 by Charlie

New Features in C# 6

c#

Tryout the new features in C# 6.0 with online code snippet runner.

1. Exception Filters

Exception filters allow you to add additional conditions to exception handlers. We can now write an if a when statement next to a catch block & the catch block will only get executed if the condition returns true. Below is an impractical & silly example to play around with.

Practical use of this would be in cases where we need to perform different actions for the same exception, based on some property of the exception (e.g Http Error Code)

try
{	
  DownloadWebPage();
}
catch(HttpException ex) when (ex.GetHttpCode() == 408)
{
  //Some Retry Logic
}
A very important advantage of exception filters is that it preserves the CLR stack. More on this: C# 6 Exception Filters and How they are much more than Syntactic Sugar


2. Using await in catch and finally blocks

We can now await functions in catch and finally blocks. This was not allowed prior to C# 6.0

3. The nameof Operator

There are times when we need the name of a variable in string form. nameof operator does just that. It takes a variable and converts the variable name to string. 

If you have implemented INotifyPropertyChanged in WPF, you would be very happy to have this feature. Below is another practical situation when we need to get the name of a variable:

4. Null Conditional Operator

Suppose we have an instance of a class Node (the class shown in above snippet). 
Now we want to print "Invalid Node" when the instance is null, or if instance's Children property is null.
Normal way of doing this is: 

if(node==null || node.Children == null)
  Console.WriteLine("Invalid Node");
The same condition can be written in C# 6.0 as 
if(node?.Children == null)
  Console.WriteLine("Invalid Code");
The ?. operator returns null if anything in the object reference chain is null. This would especially helpful with long chains.

a?.b?.c?.d? would retun null if either of a,b,c or d is null.

A practical scenario where this is useful is while firing an event. Currently before you can invoke a delegate, you need to create a local copy (to avoid race condition), check for null, and then invoke:

var localCopyOnChanged = OnChanged;
if (localCopyOnChanged != null)
{
  localCopyOnChanged(this, value);
}
With Null Conditional Operator the above code can be rewritten simply as:
OnChanged?.Invoke(this, value)

More on this: The New ?. Operator in C# 6

5. Auto Property Initializers

We can now provide initial/default values to properties (even if they are readonly) at the time of declaration.


6. String Interpolation

In String.Format function, instead of passing as parameters objects that you want to replace with indexed markers inside the string, you can now reference variables directly. Infact, you dont even need to call String.Format function. There's nothing new here, just some compiler magic. 


7. "using" statement for static classes

If you have a static class, whose members you are using a lot you can now avoid typing the class name everytime by including the class in the using declaration.


More new features in C# 6:

C# 6 Expression Bodied Methods & Properties | C# 6 Parameterless Struct Constructors, its Quirks, and Restrictions

Comments