Why use a List?
Lists are very good collections. When you create your applications there are many reasons to use a list to store objects.
If you do not know when you design an applcation how many objects of one class you need to keep track of, a list would be a very good choice for storing your objects because it is very easy to add another object to your list. If you used an array you would have to make a lot more changes to your code.
If you think, as your application grows or you upgrade your application, you will need to store more objects of one class, a list would be a good idea for storing your objects. Same reason as above.
Using lists you can write your code so that it is easy to just add another object without worrying about bounds of arrays.
How do you use a List?
Using Lists is very simple.
First you must declare your list. You do this like this:
CODE
List<class> myList = new List<class>();
There are two ways you can add a new object into your list. The first one is using the Add method of the List class like this:
CODE
myList.Add(object);
You can also add an object at a specifc point in the list using the Insert method like this:
CODE
myList.Insert(index, object);
If you use the Insert method there are two things you will need to remeber. First is that the index value is zero based. That means the first object in the list is 0, just like in arrays. The second is if you try to insert an object into a list that is outside the list you will get an exception. Say you have 10 objects in your list. If you try to insert an object at 20 you will get an exception.
The List class has a property called Count. You can use it to tell how many items are in your list. You use it like this:
CODE
int number;
number = myList.Count;
To remove an object from your list there are a few ways you can do that. The first is to use the Remove method like this:
CODE
myList.Remove(object);
This will remove the first occurance of the object in a list.
You can also chose a specific element to remove like this:
CODE
myList.RemoveAt(index);
Again, like in the Insert method you have to make sure you pass a correct index or your program will generate an exception.
There are one more methods that I want to talk about the Clear method. The Clear method removes all entries of the list. You use it like this:
CODE
myList.Clear();
There are two ways that you can go through each object in the list. First you can use a foreach loop. You would do it like this:
CODE
foreach (Class o in myList)
{
System.Console.WriteLine(o.ToString());
}
You can also use a for loop to go through the list. This is how you would do it:
CODE
for (int i = 0; i < myList.Count; i++)
{
System.Console.WriteLine(myList[i].ToString());
}
Here is a short sample Console program showing how to use methods and properties that I described above using a list of strings.
CODE
using System;
using System.Collections.Generic;
using System.Text;
namespace ListDemo
{
class Program
{
static void Main(string[] args)
{
// creating a list of strings
List<string> myList = new List<string>();
// adding objects to the list
myList.Add("Bob");
myList.Add("Tim");
myList.Add("Wendy");
// write the list
WriteListForEach();
Console.ReadLine();
// inserting a object at position 1
myList.Insert(1, "Kerry");
// write the list
WriteListFor(myList);
Console.ReadLine();
// Removing Tim from the list
myList.Remove("Tim");
// write the list
WriteListForEach(myList);
Console.ReadLine();
// Remove the item at index 1
myList.RemoveAt(1);
// write the list
WriteListFor(myList);
Console.ReadLine();
}
static void WriteListForEach(List<string> theList)
{
// write the list using a foreach loop
foreach(string o in theList)
{
Console.WriteLine(o);
}
}
static void WriteListFor(List<string> theList)
{
// write the list using a for loop
for (int i = 0; i < theList.Count; i++)
{
Console.WriteLine(theList[i]);
}
}
}
}
Lists have many other Methods. This is just a brief introduction.