0

If I have a variable, I can print its declared name out as follows in C#:

String myVar = "This is a string.";
Console.WriteLine($"This variable is named {(((MemberExpression)((Expression<Func<string>>)(()=>T)).Body).Member.Name)}.");

This prints This variable is named myVar.

However, I would need this inside the scope of its defining function, i.e.

String myVar = f(args);
public String f(args) {
    String name = ...
    //here goes any code necessary to make name = "myVar"
}

What code do I need to put inside f to recover the variable name? Is this possible at all? I presume it's a bit more complicated than the previous one.

6
  • 8
    Are you asking about nameof(myVar)?
    – user47589
    Feb 3, 2020 at 18:06
  • So you are asking, if I am a function, can I get the name of the variable to which the result of my completion will be assigned?
    – NWard
    Feb 3, 2020 at 18:07
  • Please provide sample input and expected output. your comment inside f is confusing, since myVar is the name of the variable being assigned from the result of f, and is not being passed into f.
    – Rufus L
    Feb 3, 2020 at 18:11
  • No args is a reference to data (string value), and it does not know what other references there are outside of the function pointing to the same data. Feb 3, 2020 at 18:13
  • 1
    Its odd to me that you are using such a complex construction here to get the name of a variable instead of using nameof. Can you say a bit about why you made that choice? Feb 3, 2020 at 20:08

2 Answers 2

8

Is this possible at all?

No. First, the value returned by a method need not be assigned to a variable at all. Second, even if it is assigned to a variable, not all variables have names. Therefore there is no way to know the name of the variable that the value of a function call will be assigned to.

The best practice is to always design functions so that they compute values independently of the details of the caller.

(It is possible to get the source code location of the caller from the callee, but that feature should be limited to debugging and diagnostic purposes.)

This sounds very much like an "XY" question. That is, you have some real problem, you have come up with a wrong solution to that problem, and now you are asking a question about the wrong solution. What's the real problem you are attempting to solve?

9
  • Hi Eric, why do you say that it is not possible at all ? I'm no expert. So I would like to know your thoughts please
    – Clint
    Feb 3, 2020 at 19:24
  • 1
    Because the method call need not be assigned to a variable, and even if it is, the variable might not have a name. Since the variable name might not exist there cannot be a reliable way to obtain the variable name. Feb 3, 2020 at 20:04
  • "variable name might not exist" does it refer to a scenario where you are pasing string value to GetIdentifier ("Hello") ? In my example
    – Clint
    Feb 4, 2020 at 2:54
  • 1
    @Clint: Consider: M(); the return is discarded, so there is no variable to assign to. Consider: N(M()); -- the variable assigned to is a formal parameter of N(). Consider P(params object[] x) and P(M()). The variable assigned to is x[0] -- what is the name of x[0]? Consider A returns an array and X returns an index; A()[X()]=M(); What is the name of the variable assigned to? How about *((int*)12345678) = M(); in unsafe code; what is the name of the variable? Feb 4, 2020 at 15:01
  • 1
    @Clint: Suppose M returns a sequence; what is the name of the variable assigned to in foreach(var x in M())? It is not x; x is assigned the members of the sequence, not the sequence. What is the name of the variable assigned in return M();? What about var s = from x in M() where x > 10 select x.ToString(); s is a completely different sequence than that returned by M() so that's not it. What about if (M().Count() > 10), what's the name of the variable assigned to there? There are many dozens of ways that a call is never assigned to a variable. Feb 4, 2020 at 15:04
-9

nameof exists, lets use that instead.

String myVar = "This is a string.";
Console.WriteLine($"This variable is named {nameof(myVar)}.");

So your next bit, we have to make some changes. I'm assuming since you're passing args that this is being called in your Main method. We need to make myVar a class level variable.

static String myVar;

static void Main(string[] args)
{
      myVar = f(args);
}

public static String f(string[] args) {
    String name = nameof(myVar);
    // Whatever else you're doing
}

This is a bit hacky, and relies on you only ever assigning the result of this function to a specific variable which exists outside of the function, and this is certainly against OOP principles and is a definite anti-pattern. With more information about your use case we could probably guide you to a better solution.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.