Latest C# 6.0 Features in RTM VS2015

There are plenty of blogs and articles already posted about C# 6.0 new features. But I observed there are some changes from the features in the preview and newly shipped with Visual Studio 2015 RTM (of course some improvements are based on feedback from the preview). In this article, I'm trying to address those changes as well as I will also describe new features.

The following is the list of enhancement delivered in the RTM version of C# 6.0 (excluding VB enhancements that were specified in the source, also I have added quick samples to illustrate the enhancements).

Feature Example
Auto-property initializers public int X { get; set; } = x;
Read-only auto-properties public int Y { get; } = y;
Constructor assignment to getter-only autoprops public DemoClass{
Y = 15;
}
Static imports using static System.Console; … Write(4);
Index initializer > new JObject { ["x"] = 3 }
> var numbers = new Dictionary<int, string> {
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
Await in catch/finally try … catch { await … } finally { await … }
Exception filters catch(E e) when (e.Count > 5) { … }
Partial interfaces Partial Interface I1
Multiline string literals "Hello<newline>World"
Expression-bodied members public double Dist => Sqrt(X * X + Y * Y);
Null-conditional operators customer?.Orders?[5]
String interpolation $"{p.Name} is {p.Age} years old."
nameof operator string s = nameof(Console.Write);
#pragma #Disable Warning BC40008
Read-write props can implement read-only interface properties <"In my TODO items">
#Region inside methods public void TestMethod()
{
#region MyRegion

#endregion
}
CRef and parameter name /// <summary>
/// Initialize with <see cref="Program(DateTime)"/>
/// </summary>
public Program(DateTime timestamp)
{

}
Extension Add in collection initializers See the explained example below.
Improved overload resolution <"In my TODO items">

Most of the feature enhancements specified above are self explanatory. So I'll be explaining only the interesting ones that need explanation.

Properties Enhancements

We have a couple of enhancements in the Auto properties. With the new enhancements the compiler generated the required code and saved some developer's time. Now the properties are:

  1. public class Person  
  2. {  
  3.     // Readonly properties are not allowed to be initialized in Constructor  
  4.     public int Id { get; }  
  5.    
  6.     public Person()  
  7.     {  
  8.         // Initialzing the readonly property.  
  9.         this.Id = -1;  
  10.     }  
  11.    
  12.     // Initialize the property right at the declaration  
  13.     public string Name { getset; } = "Jhon Doe";  
  14.    
  15.     public int Age { getset; }  
  16.    
  17.     public string Title { getset; }  
  18. }  
Note: Static properties are not allowed to have constructor initialization.

Static Imports and Extension methods

Now C# allows importation of static members of another class without using the class name. So they can be used directly in another class. This feature is extended to include the Extension methods. Also the improvement was done with Using. Now one must use the Static keyword to import the static members.

For example:
  1. using System.Text;  
  2. using System.Threading.Tasks;  
  3.    
  4. using static System.Console;  
  5. using static System.Linq.Enumerable;  
  6. public class Demo  
  7. {  
  8.     public void Print()  
  9.     {  
  10.         var cultures = new string[] { null"en-NZ""en-US""en-AU""en-GB" };  
  11.         Write("The static methods are directly available.");  
  12.    
  13.         // Here Range() is static so only simple static methods are allowed to use.   
  14.         // Extensions are static   
  15.         // but they are not be allowed to use directly.   
  16.         // This is taken care of very carefully to keep the   
  17.         // separation in these two types of features.  
  18.         WriteLine(Range(0, 10).Where(x => x % 2 == 0));  
  19.     }  
  20. }  
Extension Add method for Collection Initializers

This looks interesting but this feature already exists in VB.NET, surely it was brought back home to C#. It could be value added where a Generic collection must be initialized with complex types. Here's a sample of how it works.

For example the following is a simple entity representing a complex type (let's assume).
  1. public class Person  
  2. {  
  3.     public int Id { getset; }  
  4.    
  5.     public string Name { getset; }  
  6.    
  7.     public int Age { getset; }  
  8.    
  9.     public string Title { getset; }  
  10. }  
Now C# 6.0 allows us to write an extension Add method for a Collection, for example:
  1. public static class Extensions  
  2. {  
  3.     public static void Add(this List<Person> source, int id,  string name, int age, string title)  
  4.     {  
  5.         source.Add(new Person {  
  6.             Id = id,  
  7.             Name = name,  
  8.             Title = title,  
  9.             Age = age  
  10.         });  
  11.     }  
  12. }  
If we want to use this collection of type Person in the code then the collection initializer would be less work as in the following:
  1. List<Person> List = new List<Person>()  
  2. {  
  3.     { 1, "Ramesh", 22, "Mr." },  
  4.     { 2, "Julia", 24, "Ms" },  
  5.     { 3, "Paula", 12, "Mrs." },  
  6. };  
This might not look like a big change but will surely add some value in some cases, for example unit tests where collections are to be created for fake data.

I hope most of us will be enjoying these features though it will take some time to decide if they are a handy feature. Those that are marked as a “TODO” item, I'll be working on exploring them and may be writing about them in a future article.


Similar Articles