The pbook list is of type Person and stores a list of subscribers. I display using the Show method of the Phonebook class, but the Phonebook.Person link displays to me. How to display the list of subscribers in the telephone directory?

Phonebook class:

class Phonebook { private List<Person> pbook = new List<Person>(); public Phonebook() { } public void addPerson(Person p) { pbook.Add(p); } public void Show() { foreach (var person in pbook) { Console.WriteLine(person); } } public void Menu() { int control = 0; while (control != 3) { Console.WriteLine("Сделайте выбор: 1 - добавить абонента, 2 - вывести абонента, 3 - выход."); control = Convert.ToInt32(Console.ReadLine()); switch (control) { case 1: Person p = new Person(); Console.WriteLine("Введите фио:"); p.AddName(Console.ReadLine()); Console.WriteLine("Введите телефон:"); p.AddPhone(Console.ReadLine()); Console.WriteLine("Введите адрес:"); p.AddAddress(Console.ReadLine()); addPerson(p); break; case 2: Show(); break; case 3: Environment.Exit(0); break; default: break; } } } } 

Person class:

 class Person { private string Name; private string Phone; private string Address; public Person() { } public void AddName(string n) { Name = n; } public void AddPhone(string p) { Phone = p; } public void AddAddress(string a) { Address = a; } public void ShowPerson() { Console.WriteLine("ФИО: {0}, номер: {1}, адрес: {0}", Name, Phone, Address); } } 

    3 answers 3

    Is this your code?))) You need to override the toString () method in the Person class, or should you use your code

     public void Show() { foreach (var person in pbook) { Console.WriteLine(person.ShowPerson()); } } 
    • My code. And what's wrong with my code, that you have doubts, that this is not my code? I am writing for myself this telephone directory, as it is advised everywhere I chose an educational project to consolidate a theory that will be interesting. - ArniLand
    • Yes, I somehow vseravno, apparently, Cho code is educational, but your question seemed strange to you because the whole principle of OOP is not clear to you yet - Gorets
    • What exactly is not clear to me in your opinion? - ArniLand pm
    • Specifically, your code catches your eye, what you do not understand exactly what the person variable means in the lines of the foreach code (var person in pbook) {Console.WriteLine (person); }. What type it is, and what exactly it should contain. During training, I advise you not to use var, but to use exactly what this type is, in this case Person. Then it will be more clear what should be displayed on the screen. In this case, it is displayed exactly as a type, not its contents - PhoneBook.Person (Person is a type / class, and PhoneBook is not a type or class, but a namespace) - krupennikov

    A mistake that is noticeable is the dependence of the ShowPerson method on the console. This will not work in a Windows application.

    As recommended to you by krupennikov, it’s better to override the ToString method so that it returns a string — and you can write this string, for example, to the console. Instead of Show, it would probably be logical to use ToString too.

    Another confusing is the combination of while (control != 3) and Environment.Exit . Some of this is clearly superfluous, in theory, should work without Environment.Exit . If it does not work, sort out the debugger where the error is.

      The guy wrote correctly. In the class Person add

       void override ToString() { Console.WriteLine("ФИО: {0}, номер: {1}, адрес: {0}", this.Name, this.Phone, this.Address); } 

      And in the PhoneBook class, in the output of the list item, simply write:

       public void Show() { foreach (var person in pbook) { Console.WriteLine(person.ToString()); } } 
      • why add? everything is already there =) if it is more convenient for him to use his method ShowPerson (), let it be so, you just need to understand that there is also ToString () =) - Gorets
      • void override ToString() is nonsense. It cannot return void , only string . Well, the contents of the method should be different. Not Console.WriteLine , but string.Format . - Modus
      • Why is it better to override and use the ToString () method? - ArniLand
      • I'm not sure, but it seems to me that everyone knows that all objects of the language have a toString method, because loading code with one’s own method is superfluous, above, someone wrote about dependencies on consoles in different operating systems, but I’m not sure about that as they say about the source code (and not about the file) in which there is a syntax that is the same for the language, in all operating systems - Gorets