Click here to Skip to main content
13,146,014 members (43,146 online)
Click here to Skip to main content
Add your own
alternative version

Stats

21.2K views
9 bookmarked
Posted 2 Dec 2014

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

Rate this:
Please Sign up or sign in to vote.
nameof expressions in C# 6.0

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 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)

Share

About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer 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 Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:


You may also be interested in...

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
memberFernando A. Gomez F.4-Dec-14 8:30 
AnswerRe: Advantages of nameof over CallerMemberName Pin
sisnaz4-Dec-14 16:24
membersisnaz4-Dec-14 16:24 
AnswerRe: Advantages of nameof over CallerMemberName Pin
Christian Wulff23-Jul-15 2:17
memberChristian Wulff23-Jul-15 2:17 
QuestionCallerMemberNameAttribute Pin
bobonlinetsl4-Dec-14 2:34
memberbobonlinetsl4-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 3 Dec 2014
Article Copyright 2014 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid