I'm trying to make a List of about 5500 teams and create an object Team for each element of the list using this code I got from a tutorial:
public Form1()
{
InitializeComponent();
List<Team> teams = new List<Team>();
teams.Capacity = 5500;
for (int y = 1; y <= 5500; y++)
{
teams[y] = new Team(y);
}
}
But I keep getting this error:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in [my program].exe
Additional information: Index was outside the bounds of the array.
In this program, "Team" is a custom class that requires a team number when created (...new Team([teamnumber])).
Each teamnumber is their unique identity, so this has to correspond with each team's index in the list.
...teams[y] = new Team(y);...
What I'm trying to do is make sure there's a Team object created for every element in teams so that I don't run into errors later when trying to add an attribute to a certain element. Also, I don't want a "Team 0", this is why I started y at 1 in the for loop. I've also tried using a foreach loop but I get the same error.
I'm using Visual Studio 2012 Express. This is a Windows Presentation Forms program written in C#.
Team[] teams = new Team[5500];. But you can use aList<Team>, just useAddmethod to append items to your list. Note that bothList<T>andT[]are indexed from0throughN-1, not1throughN.var teams = Enumerable.Range(1, 5500).Select(y => new Team(y)).ToList();.