1
string MyVar1 = "bilah bilah";
dosometing(MyVar1);

    void dosometing(object MyObject)
    {
       string VarName = nameof(MyObject);   // it givess : "MyObject"
    }

But I was expecting "MyVar1" is there a way for that? using dynamic? or ref?

Ali CAKIL
  • 383
  • 5
  • 21
  • i don't think there's a way for that, and i don't see _any_ practical use case for this - what are you trying to achieve by that? whatever it is, i bet there's a dozen better ways at the least. – Franz Gleichmann Aug 21 '20 at 06:24
  • 4
    What do you expect to happen if the caller were `dosometing("Hello");` or `dosometing(MyVar1 + AnotherVar);`? – Sweeper Aug 21 '20 at 06:24
  • Have a look at this. Possible duplicate: https://stackoverflow.com/questions/72121/finding-the-variable-name-passed-to-a-function – rmbl Aug 21 '20 at 06:25
  • 1
    Does this answer your question? [Finding the variable name passed to a function](https://stackoverflow.com/questions/72121/finding-the-variable-name-passed-to-a-function) – Austin G Aug 21 '20 at 06:27
  • Your `MyVar1` variable is just that, a *variable*. Since `string` is a reference type, it holds a reference to that literal string. In C# parameters are passed by value, so that reference is passed by value and copied to the `MyObject` variable. At that point, there are two distinct variables that refer to the same object. Other than that, there is no relation between them. The `nameof` operator allows the compiler to provide the programmer with the name of a variable (or class or..) that is in scope. There's no way to do what you want, it doesn't match the design at all – Flydog57 Aug 21 '20 at 06:28
  • If your goal is just to play with variable name and value. you can use generic collection `Dictionary keyValuePairs = new Dictionary();` . it will serve your purpose. you don't need to use object. – SSD Aug 21 '20 at 06:49
  • How variable name is going to be used? Imho you don't need name of it. But you are using its name for something to identify. The most clean way is to add parameter to a method, where the caller define id as well as passing value. This will also solve ambiguity mentioned by @Sweeper. – Sinatr Aug 21 '20 at 07:50

2 Answers2

4

That's not possible. But you can do something like:

string MyVar1 = "bilah bilah";
dosometing(MyVar1, nameof(MyVar1));

void dosometing(string MyString, string VarName)
{
   // MyString holds the value
   // VarName holds the variable name
}
Jeethendra
  • 356
  • 1
  • 6
  • actually I m doing this, I goal was to make this code less, because I m using it a lot for database actions. Now i see it s not possible and dublicate, sorry fot that – Ali CAKIL Aug 21 '20 at 06:27
0

Maybe this information is usefull for you.

Since you want the value and the name of the property that has changed you could move the method dosomething inside the setter of the property.

(Notice: I am assuming you are actually working with properties and not local variables as shown in your question, and your question is just simplified)

So something like this:

public class Foo
{
    private string _myVar1;
    public string MyVar1
    {
        get => _myVar1;
        set
        {
            _myVar1 = value;
            DoSomething(value);
        }
    }


    private void DoSomething(string value, [CallerMemberName]string propertyName = "")
    {
         Console.WriteLine(value);
         Console.WriteLine(propertyName);
    }
}

The attribute CallerMemberName requires the using System.Runtime.CompilerServices

More Information can be found here: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callermembernameattribute

See it in action here: https://dotnetfiddle.net/YvqqdP

Rand Random
  • 7,300
  • 10
  • 40
  • 88