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. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos