New Feature of C# 6.0: Nameof Operator

Introduction

Microsoft announced the new version of C#, C# 6.0, on the day of the 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 the operator.

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

Example. nameof(Expression) as in the following

static void DisplayName(string name)
{
    if (name == null)
    {
        throw new ArgumentNullException(nameof(name));
    }
}

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 the 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 the operator in C# 6.0. Now we can see the intelligence of the nameof the operator in Visual Studio 2015 also.

IntelliSence1

The program is written in an older version of C#

Example 1

using System;

class Student
{
    static void DisplayName(string name)
    {
        if (name == null)
        {
            throw new ArgumentNullException("name");
        }
        else
        {
            Console.Write("Name: " + name);
        }
    }

    static void Main(string[] args)
    {
        Student.DisplayName("Rajesh");
    }
}
q

Program written in C# 6.0

using System;

class Student
{
    static void DisplayName(string name)
    {
        if (name == null)
        {
            throw new ArgumentNullException(nameof(name));
        }
        else
        {
            Console.Write("Name: " + name);
        }
    }

    static void Main(string[] args)
    {
        Student.DisplayName("Rajesh");
    }
}

Program is written in an older version of C#

Example 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VS2013
{
    class Book : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string b_title;

        public string Title
        {
            get { return b_title ?? ""; }
            set
            {
                if (b_title != value)
                {
                    b_title = value;
                    OnPropertyChanged("Visual C# 6.0");
                }
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

In the code above, we are implementing an interface named INotifyPropertyChanged because, in this program, we are using PropertyChanged, which 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 the 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)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VS2013
{
    class Book : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string b_title;

        public string Title
        {
            get { return b_title ?? ""; }
            set
            {
                if (b_title != value)
                {
                    b_title = value;
                    OnPropertyChanged(nameof(Title));
                }
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Now we will write a small program to to see the output of the program.

using System;
using System.ComponentModel;

namespace NameofOperator
{
    class Book
    {
        static void Display(string bname)
        {
            if (bname == null)
            {
                throw new ArgumentNullException(nameof(bname));
            }
        }

        static void Main(string[] args)
        {
            Book.Display(null);
        }
    }
}

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.


Similar Articles