BenjeminStar
BenjeminStar

Reputation: 47

How to get the name of an Object?

I am writing a new program and I want to get the name of an Object. Here my code:

class Program
{
    static void Main(string[] args)
    {
        Computer c1 = new Computer(NAME_OF_OBJECT);
    }
}

class Computer
{
    public Computer(string _ComputerName)
    {
        Console.WriteLine($"The computer {_ComputerName} has been created.");
    }
}

At the position NAME_OF_OBJECT I want to give the name c1 of the new created object Computer.

At the end this output should come out The computer c1 has been created.

But, how to do that? Is there any way to reallize that?

Upvotes: 0

Views: 167

Answers (1)

user10242112
user10242112

Reputation:

Try this:

Computer c1 = new Computer(nameof(c1));

Upvotes: 2

Related Questions