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 NowNext up:
- HR for Non-HR Managers: Is the HR function necessary?
- Microsoft Dynamics – Maximum processing power!
- The Best Excel Keyboard Shortcut
- Instant calculations in Excel
- Prototypal Inheritance in JavaScript
- HR and age discrimination
- Development Sideloading of Windows Store Apps
- The Spike
- How to read and write XML
- Background images in OneNote 2013
Previously
- HR for Non-HR Managers: Is the HR function necessary?
- Top 10 posts you may have missed from February
- Screen Clip your Internet!
- Windows To Go! Part 2.
- Hyper-V Network Types
- What is a Concordance File?
- Me, Myself and I in the workplace
- Security Compliance Manager Updated
- The Power of Zoom
- Planning Exchange Online and Configuring DNS Records