New Feature of C# 6.0: Nameof Operator

Overview

Microsoft announced the new version of C#, C# 6.0 at the day of Visual Studio Connect() event on November 13, 2014. They also announced the latest IDE of Visual Studio, Visual Studio 2015 Preview. According to the announcement, C# 6.0 came with many new features. In this article we will learn about the nameof operator.

The nameof operator allows developers to use a program element as text. Now we cannot specify any string literals directly with the nameof operator. It can be used to create the nameof expression to specify the name where expression may be a property-group or a method-group.

For example: nameof(Expression) as in the following:
  1. static void DisplayName ( string name )   
  2. {   
  3.   if (name== null)   
  4.   throw new ArgumentNullException(nameof(name));   
  5. }   
Note

Here expression must have a name and may refer to either a property-group or a method group. For a better explanation of the nameof operator, let's see an example program in which we have written the code in an older version of C# and how the code will be modified to use the nameof operator in C# 6.0. Now we can see the intelliSence of the nameof operator in Visual Studio 2015 also.

IntelliSence1

Example: I
 
Program written in older version of C#:
  1. class Student   
  2. {   
  3.   
  4.      static void DisplayName(string name)   
  5.      {   
  6.           if (name == null)   
  7.           throw new ArgumentNullException("name");   
  8.           else   
  9.           Console.Write("Name: " + name);   
  10.      }   
  11.   
  12.      static void Main(String[] args)   
  13.      {   
  14.           Student.DisplayName("Rajesh");   
  15.      }   
  16. }   
Program written in C# 6.0:
  1. class Student   
  2. {   
  3.   
  4.   static void DisplayName(string name)   
  5.   {   
  6.        if (name == null)   
  7.        throw new ArgumentNullException(nameof(name));   
  8.        else   
  9.        Console.Write("Name: " + name);   
  10.   }   
  11.   
  12.   static void Main(String[] args)   
  13.   {   
  14.      Student.DisplayName("Rajesh");   
  15.   }   
  16. }   
Example : II

Program written in older version of C#:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Linq;   
  5. using System.Text;   
  6. using System.Threading.Tasks;   
  7.   
  8. namespace VS2013   
  9. {   
  10.   class Book : INotifyPropertyChanged   
  11.   {   
  12.     public event PropertyChangedEventHandler PropertyChanged;   
  13.     private string b_tilte;   
  14.      
  15.     public string Title   
  16.     {   
  17.          get { return b_tilte ?? ""; }   
  18.          set   
  19.          {   
  20.               if (b_tilte != value)   
  21.               {   
  22.                  b_tilte = value;   
  23.                  OnPropertyChanged("Visual C# 6.0");   
  24.   
  25.               };   
  26.           }   
  27.     }   
  28.   
  29.     protected virtual void OnPropertyChanged(string bname)   
  30.     {   
  31.        if (PropertyChanged != null)   
  32.        PropertyChanged(thisnew PropertyChangedEventArgs(bname));   
  33.     }   
  34.   }   
  35.   
  36. }   
In the code above we are implementing an interface named INotifyPropertyChanged because in this program we are using PropertyChanged that belongs to this interface. Now we can do the same using the nameof operator. For more details see the following program in which the nameof operator replaces the string literal. Here the PropertyChanged event occurs when a property value changes.

Program written in Visual Studio 2015 (C# v 6.0):
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Linq;   
  5. using System.Text;   
  6. using System.Threading.Tasks;   
  7.   
  8. namespace VS2013   
  9. {   
  10.      class Book : INotifyPropertyChanged   
  11.      {   
  12.           public event PropertyChangedEventHandler PropertyChanged;   
  13.           private string b_tilte;   
  14.      
  15.           public string Title   
  16.           {   
  17.                get { return b_tilte ?? ""; }   
  18.                set   
  19.                {   
  20.                     if (b_tilte != value)   
  21.                     {   
  22.                           b_tilte = value;   
  23.                           OnPropertyChanged(nameof(Title));   
  24.   
  25.                      };   
  26.                 }   
  27.          }   
  28.   
  29.          protected virtual void OnPropertyChanged(string bname)   
  30.          {   
  31.              if (PropertyChanged != null)   
  32.              PropertyChanged(thisnew PropertyChangedEventArgs(bname));   
  33.          }   
  34.     }   
  35.   
  36. }   
Now we will write a small program to to see the output of the program.
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.   
  6. using System.ComponentModel;   
  7.   
  8. namespace NameofOperator   
  9. {   
  10.   class Book   
  11.   {   
  12.   
  13.      static void Display(string bname)   
  14.      {   
  15.        if (bname == null)   
  16.        throw new ArgumentNullException(());   
  17.      }   
  18.   
  19.      static void Main(String[] args)   
  20.      {   
  21.       Book.DisplayName(null);   
  22.      }   
  23.   
  24.    }   
  25. }   
In the preceding program we are passing the parameter as null to see the output of this program. Here we have written the code to see the exception when we pass the value as null. The following picture shows the exception.

nameof1

Summary

In this article we saw how to use the nameof operator to avoid directly specifying the string literals.

Up Next
    Ebook Download
    View all
    Learn
    View all