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. You can find a very good pocket reference for C# 6.0 here.
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:
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:
More Related
- ASP.NET MVC3 Vs MVC4 Vs MVC5 Vs MVC6
- All you need to Know about Controllers and Action Methods in ASP.NET MVC
- How to use AJAX in ASP.NET MVC Application?
- ASP.NET View Engine Vs Razor View Engine
- ViewBag Vs ViewData Vs TempData Vs Session
- All you need to know to pass Exam: 70-486 (Developing ASP.NET MVC Web Applications)
- Free Practical Guide to ASP.NET Web API