Nameof Operator: A New Feature of C# 6.0

C# nameof operator returns the unqualified string name of a variable, type, or member. In this article, we will learn how to use the nameof operator in C# code example.

Introduction

On November 12, 2014, the day of Visual Studio Connect() event, Microsoft announced Visual Studio 2015 preview with many new and exciting features for developers for testing purposes. Microsoft announced the new version of C#, C# 6.0 that came with many improvements and new features. One of the newly introduced features of C# 6.0 is the nameof Operator.

What is nameof Operator

With the introduction of the nameof operator the hard-coded string to be specified in our code can be avoided. The nameof operator accepts the name of code elements and returns a string literal of the same element. The parameters that the nameof operator can take can be a class name and all its members like methods, variables and constants and returns the string literal.

Using string literals for the purpose of throwing an ArgumentNullException (to name the guilty argument) and raising a PropertyChanged event (to name the property that changed) is simple, but error prone because we may spell it wrong, or a refactoring may leave it stale. The nameof operator expressions are a special kind of string literal where the compiler checks that you have something of the given name and Visual Studio knows where it refers to, so navigation and refactoring will work easily.

The nameof Operator can be useful with multiple scenarios, such as INotifyPropertyChanged, ArgumentNullException and reflection.

Example 1

  1. string person;  
  2. Console.WriteLine(nameof(person)); // prints person  
  3. int x = 2;  
  4. Console.WriteLine(nameof(x));     //prints x  

Example 2

  1. public operatornameof(string name) //constructor  
  2. {  
  3.    if (name == null)  
  4.       throw new ArgumentNullException(nameof(name)); // use of nameof Operator  
  5.    else  
  6.       Console.WriteLine("Name: " + name);  
  7. }  
Example 3
  1. private int _price;  
  2. public int price  
  3. {  
  4.    get  
  5.    {  
  6.       return this._price;  
  7.    }  
  8.    set   
  9.    {  
  10.       this._price = value;   
  11.       PropertyChanged(thisnew PropertyChangedEventArgs(nameof(this.price)));   //// INotifyPropertyChanged  
  12.    }  
  13. }  
Demo Application 1 using Visual Studio 2013

  1. using System;  
  2. using System.Text;  
  3.   
  4. namespace CSharpFeatures  
  5. {  
  6.     public class operatornameof  
  7.     {  
  8.         public operatornameof(string name, string location, string age)  
  9.         {  
  10.             if (name == null)  
  11.                 throw new ArgumentNullException("name");  
  12.             else  
  13.                 Console.WriteLine("\n Name: " + name);  
  14.             if (location == null)  
  15.                 throw new ArgumentNullException("location");  
  16.             else  
  17.                 Console.WriteLine(" Location: " + location);  
  18.             if (age == null)  
  19.                 throw new ArgumentNullException("age");  
  20.             else  
  21.                 Console.WriteLine(" Age: " + age);  
  22.         }  
  23.         static void Main(String[] args)  
  24.         {  
  25.             operatornameof p = new operatornameof("Abhishek""Ghaziabad""23");  
  26.             Console.ReadKey();  
  27.         }  
  28.     }  
  29. }   
Demo Application 1 using Visual Studio 2015 Preview
  1. using System;  
  2. using System.Text;  
  3.   
  4. namespace CSharpFeatures  
  5. {  
  6.     public class operatornameof  
  7.     {  
  8.         public operatornameof(string name, string location, string age)  
  9.         {  
  10.             if (name == null)  
  11.                 throw new ArgumentNullException(nameof(name));  
  12.             else  
  13.                 Console.WriteLine("Name: " + name);  
  14.             if (location == null)  
  15.                 throw new ArgumentNullException(nameof(location));  
  16.             else  
  17.                 Console.WriteLine("Location: " + location);  
  18.             if (age == null)  
  19.                 throw new ArgumentNullException(nameof(age));  
  20.             else  
  21.                 Console.WriteLine("Age: " + age);  
  22.         }  
  23.         static void Main(String[] args)  
  24.         {  
  25.             operatornameof p = new operatornameof("Abhishek""Ghaziabad""23");  
  26.             Console.Read();  
  27.         }  
  28.     }  
  29. }  

Demo Application 2 using Visual Studio 2013

  1. using System;  
  2.   
  3. namespace CSharpFeatures  
  4. {  
  5.     class Operatornameof1  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             details d = new details();  
  10.             d.Age = 23;  
  11.             d.Name = "Abhishek";  
  12.             Console.WriteLine("\n Name: {0} ", d.Name);  
  13.             Console.WriteLine(" Age: {0} ", d.Age);  
  14.             Console.ReadKey();  
  15.         }  
  16.     }  
  17.     class details  
  18.     {  
  19.         private string _Name;  
  20.         public int _Age;  
  21.         public string Name  
  22.         {  
  23.             get { return this._Name; }  
  24.             set { this._Name = value; }  
  25.         }  
  26.         public int Age  
  27.         {  
  28.             get { return this._Age; }  
  29.             set { this._Age = value; }  
  30.         }  
  31.     }  
  32. }   

Demo Application 2 using Visual Studio 2015 Preview

  1. using System;  
  2.   
  3. namespace CSharpFeatures  
  4. {  
  5.     class Operatornameof2  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             details d = new details();  
  10.             Console.WriteLine("{0} : {1}", nameof(details.Name), d.Name);  
  11.             Console.WriteLine("{0} : {1}", nameof(details.Age), d.Age);  
  12.             Console.ReadKey();  
  13.         }  
  14.     }  
  15.     class details  
  16.     {  
  17.         public string Name { getset; } = "Abhishek";  
  18.         public int Age { getset; } = 23;  
  19.     }  
  20. }   

Summary

In this article we learned how to use the nameof operator to avoid the use of hard-coded strings in our code. I hope you liked this new feature of C# 6.0 introduced by Microsoft. Don't forget to read my other articles on the series "A new feature of C# 6.0" . Share your opinion about this feature and how will you use it in your project? Your comments are most welcome.