C# 6.0 Features Overview

C# 6.0 Features.

C# is a general-purpose programming language, it’s an elegant and type-safe object-oriented language that enables developers to build a variety of robust, secure applications in .NET framework.

C# 6.0 July 2015 .NET Framework 4.6 Visual Studio 2015

Read-Only Properties

Properties are an extension of data field in C#, data fields are not directly accessible from outside class, we must use the GET, SET methods to access it.

Properties

 

 

 

 

In general, read only properties allows the properties to set only inside the scope, outside classes can only read.

Properties_PrivateSet

 

 

 

 

In C# 6.0, it is not necessary to specify the private set, by default it’s a read-only property when we don’t have a SETTER

Properties_Get

 

 

 

Auto-Property Initializer

An auto-property initializer allows you to set the value of the property at the same time you declare it in a class, in the earlier version we need to set the property value in the constructor.

In earlier version of C#

PropertySetInConstructor

 

 

 

 

 

 

In C# 6.0

Auto-Property Initializers

 

 

Expression bodied function member

Expression-bodied function members allow properties, methods, operators and other function members to have bodies as lambda like expressions instead of statement blocks. Thus, reducing lines of codes and clear view of the expressions.

In earlier version of C#, the function will be like.

Old-MethodNotanExpression

 

 

 

In C# 6.0

NewExpressionBodyFunctionMember

So, we can directly write the body as an expression here.

Using static

The using static enhancement; enables you to import the static methods of single class. Previously, the using statement imported all types in a namespace.

In earlier version

Using_Static_Old

 

 

 

C# 6.0

Using_Static_Old

 

 

 

Null-conditional operators

It’s a new feature in C# 6.0 which will bring more productivity for the developers by reducing the number of lines.

In earlier version

NullCondition_Old

 

 

In C# 6.0

NullCondition_New

 

 

String interpolation

This feature inserts values into a string with simple syntax. It is similar to string.Format, but variables may be accessed directly (not through index arguments).

In earlier version

StringInterpolation_Old

 

 

 

In C# 6.0

StringInterpolation_New

 

 

 

 

Exception filters

If you want to use Exception Filter, you have to declare it in the same line where you declared the catch block, just like this: catch (Exception ex) if (FilterIsTrue). Here, if the parenthesized expression after the “if” returns true, the associated catch block will execute. Otherwise, it will move forward.

ExceptionFilters

 

 

 

 

 

 

 

nameof Expressions

Evaluates to the name of a symbol

In earlier version

nameOf_Old

 

 

 

 

In C# 6.0

nameOf_New

 

 

 

 

Await in catch and finally blocks

In earlier version of C# itself we can use try, catch and finally blocks together but was not able to use the Async and await in catch and finally blocks, so the next version of C# 6.0 introduces the features to use the await in catch {} and finally {} blocks without any complicated structure.

The await operator is applied to a task in an asynchronous method suspend the execution of the method until awaited task completes, the asynchronous method in which await is used much be modified by the Async keyword.

AwaitInCatchAndFinally

 

 

 

 

 

 

Index initializers

Initializing dictionaries and other objects with indexers is less elegant. We are adding a new syntax to object initializers allowing you to set values to keys through any indexer that the new object has

In earlier version

IndexInitializer_Old

 

 

 

 

 

In C# 6.0

IndexInitializer_New

 

 

 

 

 

 

Conclusion

I hope this article would help to understand the features of C# 6.0, in the next article we will see the features of C# 7.0

Leave a Reply

Your email address will not be published. Required fields are marked *