0

I need help with my small program. I would like for the program to get the variable name of the class that the method is running in. Something like this:

class Program()
{
   static void Main()
   {
      Foo somethingForTest = new Foo(); //variable name = [somethingForTest] 
      Console.WriteLine(somethingForTest.Name); //OUTPUT
   }
}


class Foo
{
    public string Name;
    Foo()
    {
        this.Name = nameof(this); //Saving the variable name of this instance 
    }
}

The output should be: SomethingForTest

The problem is that when I do this: nameof(this)

The compiler says that this expression doesn't have a name

Thanks for any tips.

0 Answers0