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

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube