Coding Paradox

Blog On .Net, CSharp, Windows Phone Programming

C# 6.0 New Features

without comments

C# 6 is available with Visual Studio 2015 and it comes with some new features. To try C# 6, download and install the Visual Studio 2015 preview.

Here are some of the new features that are available in C# 6

1) Auto Property Initializers

You can now assign the value to a property directly at the declaration place. With read only properties (getter only), you can assign the value at the declaration or in the constructor

Before

C# 6

Using this type of initialisation, the setter function is not invoked internally, the backing field is directly assigned the value

2) Expression-bodied members

If you have got a property/method in your code that contains a single line of code, you can use the “=>” operator to express it instead of defining the body using curly braces

Before

C# 6

This can only be used with single line functions. Though it does not offer anything useful, it does make the code look a bit readable.

3) Using “Static” Class import

You can specify a particular type in the using statement and all the static members in that type will be available in code

4) Exception Filters

Exception Filters can be used to specify a condition for the catch block. The catch block will only be executed if the condition is true.

5) String Interpolation

Before

C# 6

6) Dictionary Initializer

The syntax for initialising dictionaries is now more readable and clear. It makes the code more easier to read

Before

C# 6

7) Await in Catch block

In the earlier versions of C#, using ‘await’ in catch and finally blocks was not available. This can be quite helpful if you want to log some exception to file/database and you don’t want to block the main thread

8) Null Conditional Operator

As programmers, we do a lot of null condition check in our code. With the new null condition operator, you can do a lot of null check in a single line of code using the “?” & “??” operator

Before

C# 6

In “employee?.Name”, if the object is not null then Name will be printed. The “??” operator can be used to print some other info if the object is null. This features saves a lots of line of code which were earlier used for null checks.

As it is still in preview mode, some features might change when the final version comes out.

Written by Mako

February 24th, 2015 at 4:46 am

Posted in C#