samedi 4 avril 2020

A way to print the name of the instance

I have a class Person:

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return "Person: " + Name + " " + Age;
    }
}

In the Main class, I display the particulars of all the instances of this class (persons):

    private static void Display(string prompt, List<Person> allPersons)
    {
        foreach (Person item in allPersons)
        {
            Console.WriteLine(item.ToString());
        }
    }

The output is: Person: John 12 and so on. I would like to be able to also show the instance name: Person: person1 John 12 Is there a way to retrieve the person1 instance name? Thank you in advance.





Aucun commentaire:

Enregistrer un commentaire