C# new features nameof operator

So C# 6 has been out for some time now but I haven't used it till recently when we switched to visual studio 2015 at work. I had read about all the features, but you can't know how awesome the are until you start using them.

nameof operator

This is what msdn says about it:

Used to obtain the simple (unqualified) string name of a variable, type, or member.

Here's some simple examples

nameof(Int32) //prints "Int32"
nameof(C) //prints "C"
nameof(customer.Name) //prints "Name"

Ok so that sounds good but where would this be used? Well I found out that there are many uses so lets get into some before and after examples.

vs 2013
if(name == null) throw new ArgumentNullException("name");

Look familiar? Well theres a better way to do it now.

vs 2015
if(name == null) throw new ArgumentNullException(nameof(name));

Now you don't have to worry about typos resulting in bugs like before. This is a simple example and very common one, but lets continue; with some more.

Imagine you have a class that implements INotifyPropertyChanged, and want to listen if the Name property has changed.

vs 2013 property changed event handler
private void Test_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
   if (e.PropertyName == "Name")
   {
       //do something  
   }
}
vs 2015 property changed event handler
private void Test_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
   if (e.PropertyName == nameof(Name))
   {
       //do something  
   }
}

It's a small change but again reduces bugs.

I've had fun introducing this into my code at work and if you haven't tried it yet you really need to.

Subscribe to Coding Adventures

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe