nameof Operator

The nameof operator was introduced in C# 6. But before I discuss what the nameof operator is for, I would like to show what life was like before the nameof operator.

Life before the nameof operator

So back in C# 2, we might have the following code in our constructor:

public class Dog
{
  public Dog(string name)
  {
    if(string.IsNullOrEmpty(name))
    {
      throw new ArgumentNullException("name");
    }
  }
}

We would have used magic strings for the null argument in question, but what if we change the argument name down the line and forget to change the string. This isn’t particularly great and is brittle as I mentioned. We could mitigate the risk of this happening by writing a unit test.

You may have built a WPF app with data binding and written code like this:

private string _fullName;
public string FullName
{
    get
    {
        return _fullName;
    }
    set
    {
        _fullName = value;
        NotifyPropertyChanged("FullName");
    }
}

We risk breaking the data binding by changing the name of the property and forgetting to update the string in the call to NotifyPropertyChanged. This might not be noticed, and we have then unwittingly introduced a bug. We could improve the situation by writing a static helper function. We might have written some code similar to this:

public static string NameOfProperty<TSource, TProperty>(Expression<Func<TSource, TProperty>> property)
{

   MemberExpression member = property.Body as MemberExpression;

   if (member == null)
   {
      const string message = "Expression {0} refers to a method.";
      throw new Exception(string.Format(message, property));
   }

   PropertyInfo propertyInfo = member.Member as PropertyInfo;

   if (propertyInfo == null)
   {
      const string message = "Expression {0} refers to a field.";
      throw new Exception(string.Format(message, property));
   }
   
    return propertyInfo.Name;
}

So the property above could then be re-written like so:

private string _fullName;
public string FullName
{
    get
    {
        return _fullName;
    }
    set
    {
        _fullName = value;
        NotifyPropertyChanged(PropertyHelper.NameOfProperty(ViewModel prop) => prop.FullName);
    }
}

Now we have avoided the magic string with this code and the brittleness of it. We have compile time safety as well, so if we change the name of the property, we are forced to fix it when NotifyPropertyChanged is called. This helper method would have come in handy when using Linq to Xml to write or read some xml.

Behold the nameof operator

But now with the nameof operator, life is so much easier. So for the constructor code, we could simply write the following:

public class Dog
{

  public Dog(string name)
  {
    if(string.IsNullOrEmpty(name))
    {
      throw new ArgumentNullException(nameof(name));
    }
  }
}

The nameof operator is evaluated at compile time, so we now have the compile time checking built in, how cool is that!

Similarly the property code can be improved in the same way:

private string _fullName;
public string FullName
{
    get
    {
        return _fullName;
    }
    set
    {
        _fullName = value;
        NotifyPropertyChanged(nameof(FullName));
    }
}

Conclusion

I think the nameof operator was a great addition to C# 6 and has definitely improved the quality of code and the robustness of the code we produce. I think the readability of the code is also improved. It stops those little bugs creeping in to code when we least expect it.