Как создать список элементов разных типов (рандомно)? - C#
Формулировка задачи:
public static ArrayList NewCollection(int i)
{
Random ran = new Random();
ArrayList arr = new ArrayList();
for (int j = 0; j < i; j++)
arr.Add(ran.Next(1, 50));
return arr;
} Mammals a1 = new Mammals("Кошка", "Муська", 3, "Хищные");
Mammals a2 = new Mammals("Дельфин", "Лори", 80, "Китообразные");
Mammals a3 = new Mammals("Тушканчик", "Туня", 0.1, "Грызуны");
Artiodactyls b1 = new Artiodactyls("Лашадь", "Стрела", 90, false);
Artiodactyls b2 = new Artiodactyls("Корова", "Ночка", 100, true);
Artiodactyls b3 = new Artiodactyls("Олень", "Иммануил", 150, true);
Birds c1 = new Birds("Орел", "Боря", 2, 100);
Birds c2 = new Birds("Синица", "Синька", 0.3, 40);
Birds c3 = new Birds("Ворон", "Дрон", 2, 50);public static void WriteMyCollection(ArrayList animArr)
{
foreach (Animals a in animArr)
Console.Write("{0}\t", a);
Console.WriteLine("\n");
}Решение задачи: «Как создать список элементов разных типов (рандомно)?»
textual
Листинг программы
class StradayuFigney
{
public string Name { get; set; }
public string Description { get; set; }
}
class myBirds : StradayuFigney
{
public int Ves { get; set; }
}
class myFather : StradayuFigney
{
public int Rost { get; set; }
}
List<StradayuFigney> list = new List<StradayuFigney>();
Random rnd = new Random(DateTime.Now.Millisecond);
StradayuFigney result = null;
for (int i = 0; i < 10; i++)
{
if (rnd.Next(0, 2) == 0) result = new myBirds { Name = "", Ves = 10 };
else
result = new myFather { Name = "", Rost = 150 };
list.Add(result);
}