Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Top New Features of C# 6.0

0.00/5 (No votes)
29 Jun 2015 1  
Here are the top new features of C# 6.0

In the 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

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 used 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:

Auto Property Initializer

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:

Exception Filters in C# 6.0

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.

Exception Filter in C# 6.0

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.

await in catch and finally

Dictionary Initializer

As opposed to the older way of initializing a dictionary, C# 6.0 introduces cleaner way for dictionary
initialization as follows:

Dictionary Initializer in C# 6.0

Previously, the same was done in the following way:

Dictionary Initializer older way

Important Note: Primary Constructor has been removed from C# 6.0. For more details, please follow here.


More Related Posts

Top 10 Interview Questions and Answers Series

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here