List<>
Written by: maffelu , 2009-05-16 16:13:12
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;
}
}
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();
}
}
myCollection.ForEach(delegate(CdCollection CD)
{
Console.WriteLine("{0} - {1} ({2})", CD.artist, CD.recordName, CD.year);
});
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);
});
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);
});
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.