What’s new in C# 6.0? - nameof Expressions


Visual Studio 2015, .NET 4.6 and C# 6.0 came up with lot many features. If you didn’t try the new IDE, download the preview version in a non-production environment to get your hands dirty with it. You will definitely love the new features.

 

Today in this post we will learn about another new feature called “nameof” expression which will surely solve most of the problems a developer faces when playing with properties and notifications.

 

 

Don’t forget to read my previous posts on this series:

 

Most of the time developer faces issues with hard coded string literals when passing them as parameter to exception object or to property change event handler. If you are a developer working in any of the XAML technologies, you might already know the pain. If becomes more harder when in different file different names were used or sometime a misspelled word used while passing the property name to the PropertyChanged event handler for sending the notification to the UI.

 

Earlier to C# 6.0, just have a flash back to see how did we implement this. We pass the string name literal to the event handler method as shown below and most of the time, there’s a possibility of misspelled word:

 

Old ways to pass string (www.kunal-chowdhury.com)

But now, things got changed. You don’t have to specify any string literals here. Wondering how this can be achievable then? Now in C# 6.0 we have a new keyword named “nameof” which you can use to create the expression to specify the name. You just have to set the property to the nameof expression and it will do the magic and return a string literal.

 

Thus, nameof(Person) will return you string literal “Person” and nameof(person.Address.City) will return you string literal “City”. Here’s a code snippet to show you the implementation steps in your code and I am sure that, this will reduce your burden over the time. Isn’t it?

 

New nameof expressions in C# 6.0 (www.kunal-chowdhury.com)

Hope you liked the feature introduced by Microsoft in C# 6.0. Don’t forget to read my other posts on the same topic “What’s new in C# 6.0?” to find out all the new features introduced in C# 6.0 with Visual Studio 2015 and .NET 4.6. This post was created demonstrating the same in Visual Studio 2015 Preview build, which might change in the final build.

 

Connect with me over Facebook, Twitter, Google+ and/or LinkedIn to get the updates that I share regularly. Subscribe to my blog’s RSS feed and Email Newsletter to get the immediate update about new article submission. Happy coding.


 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

9 comments

  1. Finally! A great improvement I was waiting for a while!
    Thanks for this topic.

    ReplyDelete
  2. This was obvious issue which had very simple fix, it took so long from 3.5 (I guess) to 6.0 to fix this, but glad this is taken care now.

    ReplyDelete
  3. How is it implemented? Like CallerMemberAttribute, where the compilter just puts a string in IL for me, or with the IL nameof() operator which just wasn't accessible from C# yet?

    ReplyDelete
    Replies
    1. Just inserts the name, unfortunately. Just like CallerMemberNameAttribute.

      Delete
  4. all new features introduced in http://www.kunal-chowdhury.com/ ; against their simplicity, they are extremely useful and helpful. writing an if statement to check if an expression is null or not, was really ornery. Also passing a string parameter was really a bug prone, and string concatenation using old ways was a bit time consuming and hard to read(in case of string.format). I think VS 2015, specifically C# 6.0 is going to be the most desirable one for .net developers

    ReplyDelete
  5. Can you please explain difference between Delegate Command and Relay Command in WPF?

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top