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.
In general, read only properties allows the properties to set only inside the scope, outside classes can only read.
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
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#
In C# 6.0
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.
In C# 6.0
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
C# 6.0
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
In C# 6.0
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
In C# 6.0
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.
nameof Expressions
Evaluates to the name of a symbol
In earlier version
In C# 6.0
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.
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
In C# 6.0
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