简体   繁体   中英

How can I get a passed parameters name in C# (with nameof)

This returns parameter. I want to get i_want_to_know_this so I don't have to press CTRL+C 8 times. (So I can loop with foreach (int parameter in items[]) )

int i_want_to_know_this;
void get_parameters_name(int parameter){
  Console.WriteLine("this parameter is called: " + nameof(parameter));
}
static void Main(string[] args){
get_parameters_name(i_want_to_know_this);
}

When you wrap it in a method, the parameter name is the name of the parameter in the method, not of what was passed in. The following line will print "i_want_to_know_this".

Console.WriteLine($"This parameter is called {nameof(i_want_to_know_this)}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM