No, It is Immpossible. You do not have knowledge of the variable names used. Such information is not transmitted to the called party.
You can achieve what you want:
private static void Test(params string[][] values) { ... } public static void Main(string[] args) { string myName = "John", myAge = "33", myAddress = "Melbourne"; Test(new string[] { nameof(myName), myName }); }
Or using the dictionary:
private static void Test(Dictionary<string, string> values) { ... } public static void Main(string[] args) { string myName = "John", myAge = "33", myAddress = "Melbourne"; Test(new Dictionary<string, string> { { nameof(myName), myName }, { nameof(myAge), myAge} }); }
Or using dynamic :
private static void Test(dynamic values) { var dict = ((IDictionary<string, object>)values); } public static void Main(string[] args) { dynamic values = new ExpandoObject(); values.A = "a"; Test(values); }
Another possibility might be to use Expression , which you pass to the method. There you can extract the variable name from the expression and execute the expression for its value.
source share