Nameof Operator: A New Feature of C# 6.0

Introduction

On November 12, 2014, the day of the Visual Studio Connect() event, Microsoft announced a 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 name of Operator.

Don't forget to read my previous posts on this series: "A new feature of C# 6.0"

What is the name of the Operator?

With the introduction of the name of the operator, the hard-coded string to be specified in our code can be avoided. The name of the operator accepts the name of code elements and returns a string literal of the same element. The parameters that the name of the 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 name of operator expressions is 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 name of the Operator can be useful with multiple scenarios, such as INotifyPropertyChanged, ArgumentNullException, and reflection.

Example 1

string person;  
Console.WriteLine(nameof(person));  // prints person  

int x = 2;  
Console.WriteLine(nameof(x));       // prints x  

Example 2

public operator nameof(string name) // constructor
{
    if (name == null)
        throw new ArgumentNullException(nameof(name)); // use of nameof Operator
    else
        Console.WriteLine("Name: " + name);
}

Example 3

private int _price;  

public int Price  
{  
    get  
    {  
        return this._price;  
    }  
    set  
    {  
        this._price = value;  
        PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Price)));  // INotifyPropertyChanged  
    }  
}  

Demo Application 1 using Visual Studio 2013.

using System;  
using System.Text;  
  
namespace CSharpFeatures  
{  
    public class OperatorNameof  
    {  
        public OperatorNameof(string name, string location, string age)  
        {  
            if (name == null)  
                throw new ArgumentNullException(nameof(name));  
            else  
                Console.WriteLine("\n Name: " + name);  
            if (location == null)  
                throw new ArgumentNullException(nameof(location));  
            else  
                Console.WriteLine(" Location: " + location);  
            if (age == null)  
                throw new ArgumentNullException(nameof(age));  
            else  
                Console.WriteLine(" Age: " + age);  
        }  
        static void Main(String[] args)  
        {  
            OperatorNameof p = new OperatorNameof("Abhishek", "Ghaziabad", "23");  
            Console.ReadKey();  
        }  
    }  
}  

Demo Application 1 using Visual Studio 2015 Preview.

using System;
using System.Text;

namespace CSharpFeatures
{
    public class OperatorNameof
    {
        public OperatorNameof(string name, string location, string age)
        {
            if (name == null)
                throw new ArgumentNullException(nameof(name));
            else
                Console.WriteLine("Name: " + name);
            
            if (location == null)
                throw new ArgumentNullException(nameof(location));
            else
                Console.WriteLine("Location: " + location);
            
            if (age == null)
                throw new ArgumentNullException(nameof(age));
            else
                Console.WriteLine("Age: " + age);
        }

        static void Main(string[] args)
        {
            OperatorNameof p = new OperatorNameof("Abhishek", "Ghaziabad", "23");
            Console.Read();
        }
    }
}

Demo Application 2 using Visual Studio 2013.

using System;

namespace CSharpFeatures
{
    class Operatornameof1
    {
        static void Main(string[] args)
        {
            Details d = new Details();
            d.Age = 23;
            d.Name = "Abhishek";
            Console.WriteLine("\nName: {0} ", d.Name);
            Console.WriteLine("Age: {0} ", d.Age);
            Console.ReadKey();
        }
    }

    class Details
    {
        private string _Name;
        public int _Age;

        public string Name
        {
            get { return this._Name; }
            set { this._Name = value; }
        }

        public int Age
        {
            get { return this._Age; }
            set { this._Age = value; }
        }
    }
}

Demo Application 2 using Visual Studio 2015 Preview.

using System;

namespace CSharpFeatures
{
    class Operatornameof2
    {
        static void Main(string[] args)
        {
            Details d = new Details();
            Console.WriteLine("{0} : {1}", nameof(Details.Name), d.Name);
            Console.WriteLine("{0} : {1}", nameof(Details.Age), d.Age);
            Console.ReadKey();
        }
    }

    class Details
    {
        public string Name { get; set; } = "Abhishek";
        public int Age { get; set; } = 23;
    }
}

Summary

In this article, we learned how to use the name of 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.


Recommended Free Ebook
Similar Articles