C# 6.0 - New features

15 May 2016

The C# 6.0 was introduced when Visual Studio 2015 was released to the market. This article aims to give a brief introduction on language features & other benefits that were included as a part of new version.

1) Roslyn Cross platform compiler:

This new compiler was designed in such a way that it can be readily utilized through API from any Integrated Development Environments. It has exposed many new services through which any environment plugin developers can utilize and create fancier on the fly code review feature and it is much faster in providing those as the developer types-in the program. More over it is cross-platform.

2) Improvements to Auto properties:

Initializers for Auto properties:

The prior c# version would require us to declare a property and define its value as a separate statements either in constructors or by accessing the private variable declared for the property like below

public string Name { get; set; } Name = "Bob";

Now the same above can be achieved in single statement

public string Name { get; set; } = "Bob";
Read-Only properties:

This auto initializers are awesome if you are familiar doing the following in prior versions.

private string _name; 
public string Name { get; } 
_name = "Bob";

These statements can be translated to following new format

public string Name { get; } = "Bob";

Really sleek ? Yep!

3) Expression Bodied Methods & properties

Expression Bodied Methods

Now you can declare your simple methods in following format

public string Hello(){  
  return "Hello"; 
public void World(){

the same can be achieved by following expression like members

public string Hello() => "Hello";
public string World() => Console.Writeline("World");

Yep! I know that you think its cool how the return statements are automatically handled based on the method's return type. 

Expression bodied properties

Similarly, we can do following for properties

public string Name => FirstName + " " + LastName;

4) Static directives for 'Using' statements:

Now you can import all static members from a namespace into another and use it without the qualifier. See the sample usage below

using static System.Console;

using Static System.Math;
public void Method()[ 

5) Null-Conditional Operators:

Checking for null values before processing a variable or property is critical for writing a robust function block of code. C# 6 has an improved way of doing this.

public int? length = colours?.Length; /*null will be assigned to int if value of colours is null*/

public Colour c = colours?[0];

6) String Interpolation:

Now you can simple format a lengthy string statements in below fashion, making it less error prone.

var s = $"{person.LastName}, {person.FirstName} is currently living in {person.Country} at {person.City}."

You can even have have expressions instead of a property in the string literal.

7) Index Initializers

The values for collections like dictionary or hastable can't be initialized when the variable is initialized. So you were doing this.

var dictionary = new Dictionary<int,string>();
dictionary.add(1, "James");
dictionary.add(2, "Jim");

But with C# 6, you can do following initialization.

var dictionary = new Dictionary<int, string>{ [1] = "James", [2] = "Jim"};

8) Filters in Exception handling

try { 
catch (SqlException ex) when (ex.Number == 2) { 
   /* ...*/
catch(SqlException ex) { 
   /* ...*/

9) Now you can use await & async in catch and finally methods.