Techbubbles

C# 6.0 Features overview

This post outlines the new features that are added to the C# new version 6.0.

Enhancement to Auto Properties:  currently auto properties are needed to have setters. This puts disadvantage to 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

clip_image002.jpg

 

 

Using Static classes – Now you can put static members directly into the scope without using their class names

clip_image004.jpg

 

 

 

 String interpolation – Existing string.format function

clip_image0025.jpg

 

 

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

clip_image0045.jpg

 

 

 

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

 

clip_image006.jpg

 

 

But now you re-write the above in single line

clip_image008.jpg

                                                                                                                                                                                                                                                                           

Index initializers – today you can initialize properties in object initializers

120914_1432_C60newfeat7.png

 

 

 

 

 

 

But indexers still have to assign in separate statements. Now you can initialize indexes inside object initializers as shown below

120914_1432_C60newfeat8.png

 

 

Null Conditional Operators – today we do null condition checking for indexers as below

120914_1432_C60newfeat9.png

 

 

 

 

In C# 6 , there is no explicit null check by using ? . Operator. The new way of writing above follows

120914_1432_C60newfeat10.png

 

 

 

It works like, if left hand side is null, the whole 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

120914_1432_C60newfeat11.png

 

 

 

Exception Filters: There are few improvements to Catching, finally. Visual basic and F# both allow 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 originally occurred.

trycatch

 

 

 

 

 

 

Await in catch and finally blocks –  

trycatch2

 

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

Leave a Reply

avatar
  Subscribe  
Notify of