saturn
saturn

Reputation: 171

list array initialization c#

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);
}

Okay the thing is, when i remove the for-loop at the top the program wont start, why? Because i need to add null to the list.

I know I'm doing this the wrong way. I just want the aif[0-4] to be there, it doesn't make sense that i have to add null objects to not get an outofrange error.

Help please?

Upvotes: 0

Views: 7903

Answers (3)

3Dave
3Dave

Reputation: 29041

When you access a list element via index like so,

var myObj = foo[4];

you're assuming that the collection foo has at least five (0,1,2,3,4) elements. You're getting an out of range error because, without the for loop, you're trying to access an element that hasn't been allocated.

There are a couple of ways to handle this. The most obvious is to use List<>.Add():

List<authorinfo> aif = new List<authorinfo>();  

aif.Add(new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844));
aif.Add(new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972);  
// ....

For a toy (homework) problem like this, though, you could just initialize the list at construction:

var authorList = new List<authorinfo>
{  
new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844)
,new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972)  
, // .....
};

One of the most useful things about List<> and other collections is that they're dynamically sized, as opposed to an array. Think of a List<> as a linked list that handles all of the node connections for you. Like a linked list, a List<> doesn't have nodes until you add them, which your for loop is doing. In an array, space for references to all elements is allocated up front, so you can immediately access them, but you can't dynamically modify the size of an array.

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160852

Just add the new object instances themselves:

  List<authorinfo> aif = new List<authorinfo>();
  aif.Add(new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844));
  //... and so on

Right now you are using null as a placeholder element which you then overwrite using the indexer - you don't have to do this (nor should you).

As an alternative and if you know your list elements in advance you could also use the collection initializer:

  List<authorinfo> aif = new List<authorinfo>()
  {
     new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844),
     new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972),
     new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844)
  };

Upvotes: 5

Tigran
Tigran

Reputation: 62248

Do it like this:

var aif = new List<authorinfo> {
        new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844),
        new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972),
        new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844),
        new authorinfo("Robinson Crusoe", "Daniel", "Defoe", 1719),
        new authorinfo("2001: A Space Odyssey", "Arthur", "Clark", 1968)
};

And you done

Upvotes: 1

Related Questions