C# 6 Features Overview

This article outlines the new features that have been added to the new C# version 6.0. The new and enhanced features are:

  1. Enhancement to Auto Properties
  2. Using Static classes
  3. String interpolation
  4. Expression-bodied methods
  5. Index initializers
  6. Null Conditional Operators
  7. Exception Filters
  8. Await in catch and finally blocks

Enhancement to Auto Properties: Currently auto properties are needed to have setters. This makes a disadvantage for immutable data types. Now C# 6 allows only getters on auto properties. C# 6 also allows initializers to auto properties as shown in the following picture:

Using Static classes: Now you can put static members directly into the scope without using their class names as in the following:

String interpolation: Existing string.format function:

With the new syntax you can define the escape characters inside { } .

Expression-bodied methods: You can use a lambda arrow to just implement the single expression. Often it looks like this:

But now we can re-write the preceding in a single line as in the following:

Index initializers: Today you can initialize properties in object initializers as in the following:

But indexers still need to be assigned in separate statements. Now you can initialize indexes inside object initializers as shown below.

Null Conditional Operators
: Today we do null condition checking for indexers as in the following:

In C# 6 , there is no explicit null check using the ? . Operator. The new way of writing the preceding is as in the following:

It works like, if the left hand side is null, the entire thing is null. Only when it is not null it does the dot(.) then it becomes as result of the operation. Delegate null checking can be written as follows:

Exception Filters: There are a few improvements to catching, finally. Visual Basic and F# both allow a catch block to filter exceptions before actually catching them and now C# allows that too, this is better than catching and re-throwing. When you re-throw the exception you lose the information about where the exception originally occurred.

Await in catch and finally blocks

With the introduction of these enhanced features in C# 6.0, developers can certainly write some cleaner code with less lines code using C# 6.0.

Similar Articles