Click here to Skip to main content
15,884,628 members
Articles / Programming Languages / C# 6.0

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

Rate me:
Please Sign up or sign in to vote.
4.90/5 (9 votes)
3 Dec 2014CPOL2 min read 30.8K   9   5
nameof expressions in C# 6.0

Image 1

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.

Image 2

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 harder when in different file different names were used or sometimes, 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 we implemented 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 a 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 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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 4 Pin
Assil27-Jun-15 5:43
professionalAssil27-Jun-15 5:43 
QuestionAdvantages of nameof over CallerMemberName Pin
Fernando A. Gomez F.4-Dec-14 8:30
Fernando A. Gomez F.4-Dec-14 8:30 
AnswerRe: Advantages of nameof over CallerMemberName Pin
sisnaz4-Dec-14 16:24
sisnaz4-Dec-14 16:24 
AnswerRe: Advantages of nameof over CallerMemberName Pin
Christian Wulff23-Jul-15 2:17
Christian Wulff23-Jul-15 2:17 
QuestionCallerMemberNameAttribute Pin
bobonlinetsl4-Dec-14 2:34
bobonlinetsl4-Dec-14 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.