Use nameof() to set a string variable to its name

December 17, 2019 c#, nameof()

This is one of those things that should have just been obvious to me but once I saw it I wondered why I had never thought to use it myself. nameof() can be used when declaring a variable to set the value of the variable to the name of the variable:

public const string MyVariable1 = nameof(MyVariable1); public static void Main() { Console.WriteLine(MyVariable1); }
Read More