Top New Features of C# 6.0 (by Imran Abdul Ghani)

In previous post, we discussed and learnt about New Features of ASP.NET vNEXT. Here in this post, we will discuss the top new features and enhancements introduced in Microsoft C# 6.0. But before we dive deeper to discuss features of latest version of C#, let’s analyze how C# evolves and what key features introduced in all previous versions.

New Features in C# 6.0

Auto Property Initializer

This feature enables us to set the values of properties right at the place where they are declared. Previously, we use constructor to initialize the auto properties to non-default value but with this new feature in C# 6.0, it doesn’t require to initialize these properties with a constructor as shown below:

Note: We can use the feature on getter/setter as well as getter only properties as demonstrated above. Using getter only helps to easily achieve immutability.

Exception Filters

Microsoft introduced this CLR feature in C# with version 6.0 but it was already available in Visual Basic and F#. In order to use Exception Filters in C#, we have to declare the filter condition in the same line as that of the catch block and the catch block will execute only if the condition is successfully met as shown below:

The above code checks if the special exception occurred is of type SqlException. If not, the exception is taken care of.

Here is another case that shows how we can check the Message property of the exception and indicate a condition appropriately.

Note: Remember that Exception Filter is a debugging feature rather than a coding feature. For a very nice and detailed discussion, please follow here.

await in catch and finally block

We frequently log exceptions to a document or a database. Such operations are resource extensive and lengthy as we would need more time to perform I/O. In such circumstances, it would be awesome on the off chance that we can make asynchronous calls inside our exception blocks. We may additionally need to perform some cleanup operations in finally block which may also be resource extensive.

Dictionary Initializer

As oppose to older way of initializing a dictionary, C# 6.0 introduces more cleaner way for dictionary

initialization as follows:

Previously same was done in following way:

Important Note:

Primary Constructor has been removed from C# 6.0. For more details, please

follow here


More Related

Top 10 Interview Questions and Answers Series:

The post Top New Features of C# 6.0 appeared first on Web Development Tutorial.

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

This site uses Akismet to reduce spam. Learn how your comment data is processed.