Joydip Kanjilal
Contributor

New features in C# 6

opinion
Feb 12, 20154 mins
Software Development

Take advantage of the new features in C# 6.0 a.k.a C# vNext to reduce code clutter

C# 6 ships with Visual Studio 2015 and comes up with some interesting new features. There are features aplenty that promotes less code clutter and writing cleaner, maintainable code. In this post, I would like to run you through some of the new features in C# language.

Exception filters

Exception filters are not new in VB – now you have this feature in C# as well. These allow you to filter exceptions in your code based on severity. Here is an example. try { //some code that might throw an exception } catch (Exception exception) if(exception.GetType() != typeof(SqlException)) { ExceptionManager.HandleException(exception); } The above code checks if the exception thrown is of type SqlException. If not, the exception is handled. Here is another example that shows how you can check the Message property of the exception object and specify a condition accordingly. try { throw new CustomException("InfoWorld"); } catch (CustomException ex) if (ex.Message == "InfoWorld") { //control will come in this catch block } catch (CustomException ex) if (ex.Message == "InfoWorld") { //control will not come in this catch block }

Support for asynchrony in catch and finally blocks

This is a great feature indeed. We often log exceptions to a file or a database. Such operations are resource intensive and time consuming as you would need to access the disk to perform I/O. In such situations, it would be great if you can make asynchronous calls inside your exception blocks. You might also need to perform some cleanup operations in the finally block which might be resource intensive and/or time consuming. With C# 6 you no longer need to block the current thread while performing such resource intensive or time consuming operations. The code snippet given next illustrates how you can use await keyword in catch and finally blocks. public async Task ProcessAsync() { try { //some code that might throw an exception } catch { await Task.Delay(5000); } finally { await Task.Delay(1000); } } The following code snippet shows you can await a call to the LogExceptionAsync() custom method to log exception. try { //code that might throw an exception } catch (Exception exception) { await LogExceptionAsync(exception); }

Support for static “using” statements

This is another nice new feature in C# 6 that allows you to invoke a static method sans the need of explicit references. Here is an example. using System; using System.Console;

public class Program { private static void Main() { WriteLine("New features in C# 6"); } } As you can see in the above code snippet, you no longer need to explicitly specify the type when calling the static WriteLine() method that belongs to System.Console class. In essence, this feature promotes cleaner code – code that is easier to read, write and maintain.

Auto property initializers

This feature enables you to set the values of properties right at the place where they are declared. class Customer { public string FirstName { get; set; } = "Joydip"; public string LastName { get; set; } = "Kanjilal"; public int Age { get; set; } = 44; } In the earlier versions of C# you have had to often use default constructors to set default values to the properties in the class. Here’s another example that illustrates a shortcut syntax to initialize a property at declaration point for which a setter hasn’t been defined. class LogManager { public static LogManager Instance { get; } = new LogManager(); }

Dictionary initializers

This feature enables you to initialize default values in a Dictionary with much less code. Here is an example that illustrates this. class Program { static void Main(string[] args) { Dictionary<string, string> dict = new Dictionary<string, string>() { ["USA"] = "Washington DC", ["England"] = "London", ["India"] = "New Delhi" }; } } As you can see in the above code snippet, the dictionary has been initialized with default values at the point where it has been declared. A much nicer approach compared to the earlier versions of the C# language, isn’t it?

Primary constructor

This again is an excellent new feature – it eliminates the pain of having to write code to initialize data members of a class from the parameters in a constructor method. In other words, this feature provides a syntactic shortcut for the definition of a constructor in a class. Here is an example that illustrates how primary constructors can be used. class Employee(string firstName, string lastName, string address) { public string FirstName { get; set; } = firstName; public string LastName { get; set; } = lastName; public string Address { get; set; } = address; } You can refer to this MSDN article for more information on the new features and enhancements in C# 6.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author