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 [email protected].

comments powered by Disqus


  • .NET Core Ranks High Among Frameworks in New Dev Survey

    .NET Core placed high in a web-dominated ranking of development frameworks published by CodinGame, which provides a tech hiring platform.

  • Here's a One-Stop Shop for .NET 5 Improvements

    Culled from reams of Microsoft documentation, here's a high-level summary of what's new for performance, networking, diagnostics and more, along with links to the nitty-gritty details for those wanting to dig in more.

  • Azure SQL Database Ranked Among Top 3 Databases of 2020

    Microsoft touted the inclusion of Azure SQL Database among the top three databases of 2020 in a popularity ranking by DB-Engines, which collects and manages information about database management systems, updating its lists monthly.

  • Time Tracker Says VS Code Is No. 1 Editor for Devs, Some Working 15+ Hours Per Day

    WakaTime, which does time tracking for programmers, released data for 2020 showing that Visual Studio Code is by far the top editor/IDE used by its coders, some of whom are hacking away for more than 15 hours per day.

Upcoming Events