I would like to get the name of a variable or parameter:
For example if I have:
var myInput = "input";
var nameOfVar = GETNAME(myInput); // ==> nameOfVar should be = myInput
void testName([Type?] myInput)
{
var nameOfParam = GETNAME(myInput); // ==> nameOfParam should be = myInput
}
How can I do it in C#?
Pre C# 6.0 solution
You can use this to get a name of any provided member:
To get name of a variable:
To get name of a parameter:
C# 6.0 and higher solution
You can use the nameof operator for parameters, variables and properties alike: