C# Corner

C# 6.0 Gets More Concise with Expression Bodied Properties, Dictionary initializer

Eric Vogel goes over a few C# 6.0 language improvements that will help make your coding experiences more concise.

C# 6.0 is slated for official release with Visual Studio 2015 later this year. As of now the first release candidate version is available at https://www.visualstudio.com/downloads/visual-studio-2015-downloads-vs.aspx. The sixth iteration of C# brings many small improvements to the language syntax that when combined will make your code more concise and easier to read. Today I'll be covering a handful of these improvements such as expression bodied properties and functions, using static directive, string interpolation, and the new dictionary initializer syntax.

First let's take a look at expression bodied properties and functions. An expression bodied property is declared as a lambda expression.

For example let's say I have a Car class that has Make, Model, and Year properties. I can then declare an expression bodied property named Description that includes each property nicely formatted using an expression bodied property with string interpolation:

public class Car
    public string Make { get; set; }
    public string Model { get; set; }

    public int Year { get; set; }

    public string Description => $"{Year} {Make} {Model}";

Prior to C# 6, the Description property would be declared as:

public string Description { get { return string.Format("{0} {1} {2}", Year, Make, Model); } }

You can see how the new syntax is both more concise and readable than the old property declaration.

Similarly functions can also be declared as expression bodies:

public override string ToString() => $"{Year}-{Make}-{Model}";

You can see how string interpolation makes formatted strings much more readable. You can also use string interpolation on object instances:

Car myCar = new Car { Year = 2015, Make = "ACME", Model = "Roadster" };
Console.WriteLine($"My car is a {myCar.Description}");

Furthermore we can clean up the call to Console.WriteLine to only be WriteLine by utilizing the using static directive on System.Console:

using static System.Console;

Then you can call WriteLine as if it were declared as a local class function:

WriteLine($"My car is a {myCar.Description}");

Now let's take a look at the new Dictionary initializer. Prior to C# 6 you can declare an initialized Dictionary like this:

var items = new Dictionary<string, int> { { "Book", 2 }, { "Blu Ray Player", 1 } };

In C# 6 you can use the new [] initializer syntax as follows:

var moreItems = new Dictionary<string, int>
    ["Book"] = 2,
    ["Blu Ray Player"] = 1

You can see how the new Dictionary initializer syntax is closer to how you would access the items in the dictionary after it is cleared, thus making it easier to read.

You can see how the new language features are subtle improvements that, when combined, will help make your code easier to maintain and read. Also see Peter Vogel's articles on some of the other new features in C# 6, such as the nameof operator and the null check initializer.

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at vogelvision@gmail.com.

comments powered by Disqus


  • What's New in Visual Studio 2019 v16.5 Preview 2

    The second preview of Visual Studio 2019 v16.5 has arrived with improvements across the flagship IDE, including the core experience and different development areas such as C++, Python, web, mobile and so on.

  • C# Shows Strong in Tech Skills Reports

    Microsoft's C# programming language continues to show strong in tech industry skills reports, with the most recent examples coming from a skills testing company and a training company.

  • Color Shards

    Sharing Data and Splitting Components in Blazor

    ASP.NET Core Version 3.1 has at least two major changes that you'll want to take advantage of. Well, Peter thinks you will. Depending on your background, your response to one of them may be a resounding “meh.”

  • Architecture Small Graphic

    Microsoft Ships Preview SDK, Guidance for New Dual-Screen Mobile Era

    Microsoft announced a new SDK and developer guidance for dealing with the new dual-screen mobile era, ushered in by the advent of ultra-portable devices such as the Surface Duo.

  • How to Create a Machine Learning Decision Tree Classifier Using C#

    After earlier explaining how to compute disorder and split data in his exploration of machine learning decision tree classifiers, resident data scientist Dr. James McCaffrey of Microsoft Research now shows how to use the splitting and disorder code to create a working decision tree classifier.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events