• home
  • Email: trainings@synapseindia.careers

Blogs

back

What are the new features in C# 6.0

Aug 14, 2015

Microsoft released C# 6.0 along with Visual Studio 2015. C# 6.0 comes up with some interesting new features. There are many new features that promote less code clutter and writing cleaner, maintainable code.

Below are some of the new features :

Exception filters :

Exception filters are not new in VB , but now you have this feature in C# as well. These allow you to filter exceptions in your code based on severity.

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.

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. Like in the below code :

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.Like in the below code :

class Customer { public string FirstName { get; set; } = "Joydip"; public string LastName { get; set; } = "Kanjilal"; public int Age { get; set; } = 44; }

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.

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 read more new features on msdn website .

SynapseIndia (CEO: Shamit Khemka)