Indexed member initialisers in C# 6

 Mar 04, 2015

The new C# 6 compiler was released as open source on 1st April, 2014 in CodePlex. It was included in Visual Studio 14 CTP 3 that was released on the 18th August, 2014. The RTM of the Visual Studio 14 is expected to be released early in 2015. In today's blog post, I will introduce you to one of the exciting new language features of C# 6, indexed member initialisers.

Let's say we have a Dictionary<string,string> called MyBusinessObjects where we store for each type of business object a short description of it. Using the new C# 6 syntax, we can initialise our dictionary as follows:

Dictionary<string,string> MyBusinessObjects = new Dictionary<string,string>{ $Customer = "A customer can be an individual or a company", $Product = "Every Product is identified by 2 letters followed by 4 digits ", $Order = "One Order can include many products but only one customer" };
To refer to the elements on MyBusinessObjects, we can write:
MyBusinessObjects.$Customer = "A more precise specification of a customer object"; string productSpec = MyBusinessObject.$Product;
If you compare with the currently available syntax, you see that you write less and the code is still very readable. The $ operator only applies to strings, but the new syntax extends to any types.

For example, let's use an int as the key type of a dictionary.

Dictionary<int,string> Lines = new Dictionary<int,string>{ [1] = "First line", [2] = "Second line" };
The access to the members is the same as with the current version of C#, Lines[1], Lines[2], etc. We can't use the $ in this case, but the initialisation is simpler than the old way.

The indexed member syntax is not only for dictionaries. As another example, consider a JSON representation of an object:

String jsonCustomer = @"{ "ID" : "1001", "Name" : "John Doe", "Address" : "123 Clarence Street" };
We can parse and then access this object as follows:
JObject cust = JObject.Parse(jsonCustomer); Console.WriteLine("ID: {0}n Name: {1}n Address: {2}", cust.$ID, cust.$Name, cust.$Address);
C# 6 adds a few new features to  C#, but of course, everything you have written in the current version works fine in C# 6. To get started with  C#, I recommend that you take a look at the 20483: Programming in C# in Visual Studio 2012 training course at New Horizons.

How do your Excel skills stack up?   

Test Now  

About the Author:

Newton Godoy  

With over 17 years of in-class training experience and over 16 years of industry experience, Newton offers students a wealth of real-world technical knowledge and expertise in the areas of .NET application development, SQL Server and SharePoint Server. After spending several years lecturing as a professor, Newton found his true calling and began his career as a MCT. He worked as a technical trainer for some of Brazil's and Australia’s largest corporate training organisations before finally finding a home with New Horizons where he is now one of our top trainers. Newton brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. His combination of technical knowledge and instructor experience make him one of the most respected instructors within the IT training industry.

Read full bio
top
Back to top