The nameof operator

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /var/www/ on line 524

The next release of C# 6 has some amazing new features. In a series of blog posts I will cover some of them.

The nameof operator takes a class, method, property, field or variable and returns the string literal.

var p = new Person();


// Output:
//   Person
//   p
//   Name
//   HomeAddress

This is handy when doing input validation by keeping the method parameter and the parameter name of the ArgumentNullException in sync.

public Point AddPoint(Point point)
  if (point == null)
    throw new ArgumentNullException(nameof(point));

The nameof operator is useful when implementing the INotifyPropertyChanged interface

public string Name
    return _name;
    _name = value;

The Chained null checks blog post shows how to simplify triggering event in the OnPropertyChanged with the null-conditional operator.