A List
is just the same as creating any other object, the difference is that it's a Generic type, which means that you specify what kind of objects the List holds between the '<' and '>' characters. So the general syntax for creating a list is:
List<TypeOfObjectsItHolds> nameOfTheListVariable = new List<TypeOfObjectsItHolds>();
For example, if you want a List of integers:
List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
{
list.Add(i * 2);
}
Or a list of strings:
List<string> list = new List<string>();
for (int i = 0; i < 10; i++)
{
list.Add(string.Format("The value is {0}", i * 2));
}
Or your own class:
List<MyClass> list = new List<MyClass>();
for (int i = 0; i < 10; i++)
{
list.Add(new MyClass(i));
}
After that, you can think of a list as a "stretchy array" - you can do anything with it that you can do with an array but you can also add and remove items at runtime:
if (list.Count > 5)
{
Console.WriteLine(list[4]);
list.RemoveAt(3);
Console.WriteLine(list[4]);
}
They are really handy when you don't know in advance how many items you want to hold - for example when you want to get a load of numbers from the user and then sort them into descending order. Rather than asking the user how many items he is going to enter, you just add them to a list until he says "that's your lot!".
There are some caveats regarding performance however, which I'll give you a link to - but "remember" it for later when you are more familiar with C# and collections generally:
List<T> - Is it really as efficient as you probably think?[
^] rather than read it now!