1.

Role of nameof keyword in C#

Answer»

The unqualified string name of a variable, MEMBER TYPE ETC. can be obtained using the nameof keyword. While renaming the definitions, the code can be kept valid using the nameof keyword. This is a compile-time feature.

A program that demonstrates the nameof keyword in C# is given as FOLLOWS:

using System; class Demo {    public static void Main()    {        int number = 60;        Console.WriteLine(nameof(number));        Console.WriteLine(number);        var FRUIT = "cherry";        Console.WriteLine(nameof(fruit));        Console.WriteLine(fruit);    } }

The output of the above program is as follows:

number 60 fruit cherry


Discussion

No Comment Found