Как реализовать динамический массив в C#
Имеется следующая программы. В начале пользователь вводит число, которому соответсвует количеству записей, которые будет отображать в консоле. Затем пользователь последовательно вводит данные в консоль. Вопрос: Как сделать так чтобы пользователь сначало вводил все данные, а уже потом сразу все данные отображались в консоли в виде списка:
namespace double_linked_list
{
struct Info
{
public string count_employees;
public string status;
public string year;
public string day;
public string mounth;
public string expenses;
public void GetInfo()
{
Console.WriteLine($"Статус: {status}\nКоличество сотрудников: {count_employees}\nСрок выполнения работы: {year}/{mounth}/{day}\nМатериальные затраты: {expenses} руб.\n");
}
}
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Info myInfo = new Info();
Console.WriteLine("Введите количество записей: ");
int n = Convert.ToInt32(Console.ReadLine());
int count = 0;
while (count < n)
{
count++;
Console.WriteLine("Введите статут: ");
myInfo.status = Console.ReadLine();
Console.WriteLine("Введите кол-во сотрудников: ");
myInfo.count_employees = Console.ReadLine();
Console.WriteLine("Укажите срок выполнения работы(Год): ");
myInfo.year = Console.ReadLine();
Console.WriteLine("Укажите срок выполнения работы(месяц): ");
myInfo.mounth = Console.ReadLine();
Console.WriteLine("Укажите срок выполнения работы(день): ");
myInfo.day = Console.ReadLine();
Console.WriteLine("Укажите материальные затраты(руб): ");
myInfo.expenses = Console.ReadLine();
myInfo.GetInfo();
list.Add(myInfo);
}
foreach (object o in list)
{
Console.WriteLine(o);
}
Console.ReadKey();
}
}
}
Ответы (1 шт):
Автор решения: Aziz Umarov
→ Ссылка
попробуйте так
namespace double_linked_list
{
struct Info
{
public string count_employees;
public string status;
public string year;
public string day;
public string mounth;
public string expenses;
public void GetInfo()
{
Console.WriteLine($"Статус: {status}\nКоличество сотрудников: {count_employees}\nСрок выполнения работы: {year}/{mounth}/{day}\nМатериальные затраты: {expenses} руб.\n");
}
}
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
do
{
Info myInfo = new Info();
Console.WriteLine("Введите статут: ");
myInfo.status = Console.ReadLine();
Console.WriteLine("Введите кол-во сотрудников: ");
myInfo.count_employees = Console.ReadLine();
Console.WriteLine("Укажите срок выполнения работы(Год): ");
myInfo.year = Console.ReadLine();
Console.WriteLine("Укажите срок выполнения работы(месяц): ");
myInfo.mounth = Console.ReadLine();
Console.WriteLine("Укажите срок выполнения работы(день): ");
myInfo.day = Console.ReadLine();
Console.WriteLine("Укажите материальные затраты(руб): ");
myInfo.expenses = Console.ReadLine();
list.Add(myInfo);
Console.WriteLine("Нажмите ввод для добавления нового");
} while (Console.ReadLine() != null)
foreach (object o in list)
{
object.GetInfo();
}
Console.ReadKey();
}
}
}