C# 6.0 Feature : 'NameOf' Operator



'NameOf' Operator


Another new feature of C#6.0 is 'nameof' operator. By using 'nameof' opeator, you can remove hard coded string from code.

The ‘nameof’ operator accepts the name of code elements and returns a string literal of the same element. 

You can see in below example:

using System;

namespace NewInCSharp6
{
    class TestingNameOfOperator
    {
        static void Main(string[] args)
        {
            Person d = new Person();
            Console.WriteLine("{0} : {1}", nameof(Person.Name), d.Name);
            Console.WriteLine("{0} : {1}", nameof(Person.Age), d.Age);
            Console.ReadKey();
        }
    }
    class Person
    {
        public string Name { get; set; } = "Peter Hansen";
        public int Age { get; set; } = 40;
    }

}



C# 6.0 Feature : 'NameOf' Operator C# 6.0 Feature : 'NameOf' Operator Reviewed by kamal kumar das on December 06, 2016 Rating: 5

1 comment:

ads 728x90 B
Powered by Blogger.