melster
melster

Reputation: 1

C# class function to give out the name

I think the easiest way to explain my problem is with an example.

class MyClass
{
    string SomeFunction()
    {
          //magic 
    }
}

Void Main
{
    MyClass test = new MyClass();
    Console.WriteLine( SomeFunction );
}

So the thing I want “SomeFunktion” to is to give back the name of MyClass. In this case it would be “test”.

Is it even possible?

Upvotes: 0

Views: 60

Answers (1)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

BTW it should be Console.WriteLine(test.SomeFunction());

and

No you cannot do that unless you are using C# 6.

In C# 6, you have an operator nameof where you can pass instance and it returns name of instance like in your case "test" like nameof(test) and it would return "test".

Upvotes: 4

Related Questions