My code is as follows :
for(int i=1; i < 21; i++)
{
list.Add(random.Next(100));
}
for(int i=1; i < 21; i++)
{
Console.Write(list[i] + ", ");
}
However I am gettin’ an Argument out of range exception for i.
If I change the 2nd loop values of i to 0 and 20 it works perfectly, how so?
When you’re building the list, you’re adding 20 items.
However, a list is 0-indexed.
When you say
list[1]you’re actually getting the second element in the list. Solist[20]would actually be element 21, which doesn’t exist.