MorkaLork Development

Interesting stuff I've picked up over the years...

List<>

2009-05-16 16:13:12 | 281 views | list generic list collection

The Basics



List is a part of the new generic implementation in C#. I'm not gonna go into generics specifically, so read other guides for that.

A list is more or less an advanced multidimensional array. It's easy to use when you know it and has alot of built-in methods to use, like foreach, find, findall, sort etc. As of C# 3.0 I've come to understand that several of these features are reduntant, but I can't verify this yet, so I'll use them in this example, and in case of changes, this tutorial will be updated.

Getting into it



Allright, first I'll paste an example of how to use a list, and afterwards I'll explain it.
We'll make a list of cd's. I only have 4 cd's, so it will be easy. First we create a class to define the cd's:



public class CdCollection
{
public string artist;
public string recordName;
public int year;


public CdCollection(string artist, string recordName, int year)
{
this.artist = artist;
this.recordName = recordName;
this.year = year;
}
}


The we use this class in the list below:



class Program
{
public static void Main(string[] args)
{
//Create an instance of the list
List<CdCollection> myCollection = new List<CdCollection>();

//Add cd's to the list using the CdCollection class
myCollection.Add(new CdCollection("Metallica", "Black Album", 1991));
myCollection.Add(new CdCollection("Tom Lehrer", "An Evening Wasted with Tom Lehrer", 1959));
myCollection.Add(new CdCollection("Green Jelly", "Cereal Killer", 1993));
myCollection.Add(new CdCollection("The Beatles", "White Album", 1968));

//Create a good looking topic =)
Console.WriteLine("My collection of CD's:");
Console.WriteLine(""); //to get a blank line between topic and results.

//Output the list.
myCollection.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});


//New topic!
Console.WriteLine("");
Console.WriteLine("Sorted list (by artist):");
Console.WriteLine("");

//Sort the list by comparing names of the artists
myCollection.Sort(delegate(CdCollection CD, CdCollection CD2)
{
return CD.artist.CompareTo(CD2.artist);
});

//Done

//Output the list
myCollection.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});

//New topic!
Console.WriteLine("");
Console.WriteLine("Sorted list (by year)");
Console.WriteLine("");

//Sort the list
myCollection.Sort(delegate(CdCollection CD, CdCollection CD2)
{
return CD.year.CompareTo(CD2.year);
});
//Output the list
myCollection.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});

//New topic!
Console.WriteLine("");
Console.WriteLine("CD's from before 1990:");
Console.WriteLine("");

/*We create a temporary list with only the records of
cd's from before 1990. */
Listt<CdCollection> EarlyCd = myCollection.FindAll(delegate(CdCollection CD)
{
return CD.year < 1990;
});
//Output the temporary list
EarlyCd.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});


Console.Read();

}
}


Let's start with the list<> part, on line 5.
Listt<CdCollection> myCollection = new Listt<CdCollection>();

Here we create a list. As we can see, the list uses the CdCollection class, but this could also be a datatype, like string or integer.

Then we add 4 cd's to the list using the class CdCollection as a referance, as can be seen in the code below:

Intellisense shows the constructor inputs

After we've added the cd's we want to output them. In an ordinary array[] you can use foreach to output every part of that array, the same goes for list, only differance is that here you can do it with the lists own foreach method.

We can also sort the list as can be seen on line 30 and down. We use the lists own sorting method where we set the perimiters ourself.

This is how you output a list<T>:


myCollection.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});


This is how you sort and output a list:


myCollection.Sort(delegate(CdCollection CD, CdCollection CD2)
{
return CD.artist.CompareTo(CD2.artist);
});

myCollection.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});


And to output only items before 1990:


List<CdCollection> EarlyCd = myCollection.FindAll(delegate(CdCollection CD)
{
return CD.year < 1990;
});
//Output the temporary list
EarlyCd.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});



Output



This is what the output will look like:

This console application created with C# lists


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.