List<authorinfo> aif = new List<authorinfo>();
for (int i = 0; i < 5; i++)
{
aif.Add(null);
}
aif[0] = new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844);
aif[1] = new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972);
aif[2] = new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844);
aif[3] = new authorinfo("Robinson Crusoe", "Daniel", "Defoe", 1719);
aif[4] = new authorinfo("2001: A Space Odyssey", "Arthur", "Clark", 1968);
//authorinfo ai = new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844);
foreach (authorinfo i in aif)
{
Console.WriteLine(i);
}
さて、私が最初にforループを削除すると、なぜプログラムが起動しないのでしょうか?私はリストにnullを追加する必要があるので。リスト配列の初期化c#
私はこれを間違った方法でやっていることを知っています。私はちょうどaif [0-4]がそこに来るようにしたい、それはoutofrangeエラーを取得しないためにnullオブジェクトを追加しなければならないという意味にはなりません。
助けてください?
既に与えられた良い答えに加えて、リストの容量として使用するintをリストのコンストラクタに渡すことができます。 –