New C# 6.0 features

Posted by Anuraj on Friday, November 14, 2014 Reading time :2 minutes

.Net ASP.Net CodeProject Miscellaneous Visual Studio

As part of the Connect(); event, Microsoft introduced VS2015 Preview, which includes C# 6.0 with few more features. (These features are not included in my earlier post What is new in C# 6.0, as I already mentioned these features introduced by Microsoft in the Connect(); event few days back.)

  • nameof operator - nameof operator allows developers to use program elements as text. In MVVM project, you are using property notifications, it is recommended to use strongly typed property notifications, instead of string. Because you are hard coding the property name, if you rename the property, you need to change the string manually. The nameof operator helps you to use the property as the parameter.
public string FirstName
    get { return _firstName; }
        if (_firstName != value)
            _firstName = value;

Runtime will replace the property with the property name while generating the IL code.

IL code generated for nameof operator

  • null conditional dot operator - as the name indicates it for null checking. This helps you to make null checking fade into the background.
static void PrintBook(Book book)
    var name = book.Name;
    var price = book.Price;
    Console.WriteLine("Name :{0} - Price {1}", name, price);

I have a function like this, which will print the name and price of the book. Please note I am not doing any null check, if the book instance is null, it will throw exception. The null conditional dot operator helps to avoid this validation and processing. If you are invoking a property of null object instance, it will return null for that property as well, it won’t throw null reference exception.

static void PrintBook(Book book)
    var name = book?.Name;
    var price = book?.Price;
    Console.WriteLine("Name :{0} - Price {1}", name, price);

In case of value types, Visual Studio will treat the type of the variable as nullable, if you use null conditional dot operator.

null conditional dot operator - with value types

  • string interpolation - Another cool feature, which will help you to manage string formatting easy. In the current framework, you need use string.Format function, it little complex, you need to put numeric place holders ({n}) and based on the number you need to set the variables. String Interpolation will help developers to use actual variables as placeholders.
var message = string.Format("Book Name :{0} Price :{1}", name, price);

Can be re-write like this.

var message = "Book Name :\{name} Price :\{price}";

It supports IntelliSense for the variable names, also variables can be identified with different color.

String Interpolation - IntelliSense

You can download VS 2015 Preview from here

Happy Coding :)

What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.

Similar Posts

Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon

BMC logoBuy me a coffee

Copyright © 2019 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub