Написать структуру которая принимает много контактов
У меня есть структура которая хранит контакты(Имя, фамилия, номер телефона, адрес). Как сделать структуру которая будет принимать столько контаков сколько пользователь хочет, и будет их принимать и распечатает в консоли
public static void Main()
{
Console.WriteLine("Как вас зовут ?");
string first_name = Console.ReadLine();
Console.WriteLine("Как ваша фамилия ?");
string last_name = Console.ReadLine();
Console.WriteLine("Скажите ваш номер телефона");
int phone_number = int.Parse(Console.ReadLine());
Console.WriteLine("По какому адрес вы проживаете ?");
string addres = Console.ReadLine();
Contact info = new Contact(first_name,last_name,phone_number,addres);
info.WriteInfo();
}
struct Contact
{
string first_name;
string last_name;
int phone_number;
string addres;
public Contact(string first_name, string last_name, int phone_number, string addres)
{
this.first_name = first_name;
this.last_name = last_name;
this.phone_number = phone_number;
this.addres = addres;
}
public void WriteInfo()
{
Console.WriteLine("Вас зовут {0} {1} \nВаш телефонный номер {2} \nВаш адрес {3}",first_name,last_name,phone_number,addres);
}
}
Ответы (1 шт):
Автор решения: Deni _
→ Ссылка
public class ContactList
{
private List<Contact> _contacts = new List<Contact>();
public void AddContact(string first_name, string last_name, int phone_number, string addres)
{
_contacts.Add(new Contact(first_name, last_name, phone_number, addres));
}
public void PrintList()
{
_contacts.ForEach(contact => Console.WriteLine(contact));
}
}
в Main
var contactList = new ContactList();
contactList.AddContact(first_name, last_name, phone_number, addres);
contactList.AddContact(first_name, last_name, phone_number, addres);
contactList.PrintList();