When I do:
List<Person> person = new List<Person>();
person.Print();
I do not get anything (null)
However, when I do:
Person[] person2 = new person[10];
person2.Print();
I get an Exception.
Why? what happens in the memory? I dont get it.
When I do:
List<Person> person = new List<Person>();
person.Print();
I do not get anything (null)
However, when I do:
Person[] person2 = new person[10];
person2.Print();
I get an Exception.
Why? what happens in the memory? I dont get it.
Ok,
var people = new Person[10]
is roughly equivalent to
var people = new List<Person>
{
default(Person),
default(Person),
default(Person),
default(Person),
default(Person),
default(Person),
default(Person),
default(Person),
default(Person),
default(Person)
}
now, I suspect your implementation of Print
, which presumably extends IList<Person>
doesn't handle default(Person)
.
var people = new List<Person>();
is actually more equivalent to
var people = new Person[0];
Does your Print
implementation work with that?
With new person[0] you are initializing it with 0 references. But if you use 10, you are creating 10 references to null. The List contains nothing.
I think in your Print method your are doing a loop over the collection and with the List you don't have nothing to loop, but with the array you loop over 10 null references, which causes you Exception.