In this article we will see some common operations like search, sort, loop and manipulating lists. Since this article focuses on demonstrating some common operations of the Generics List(T) class, I have decided to keep the sample as simple as possible and will go ahead with a console application. I assume you are familiar with the features of C# 2.0 and VB.NET and understand Generics in particular.
To create a console application, open Visual Studio 2005/2008 > File > New Project > Select your desired Language and in the template pane, select Console application.
I will be using a collection of a ‘Person’ class and store it in the List(T). To add a Person class to your application, right click your project > Add > Class > rename the class to Person.cs or Person.vb. Add the following properties to the Person class:
using System;
using System.Collections.Generic;
using System.Text;
namespace CommonGenericOperations
{
public class Person
{
public Person()
{
}
public Person(int id, string first_name, string mid_name, string last_name, short age, char sex)
{
this.p_id = id;
this.first_name = first_name;
this.mid_name = mid_name;
this.last_name = last_name;
this.p_age = age;
this.p_sex = sex;
}
private int p_id = -1;
private string first_name = String.Empty;
private string mid_name = String.Empty;
private string last_name = String.Empty;
private short p_age = 0;
private char? p_sex = null;
public int ID
{
get
{
return p_id;
}
set
{
p_id = value;
}
}
public string FirstName
{
get
{
return first_name;
}
set
{
first_name = value;
}
}
public string MiddleName
{
get
{
return mid_name;
}
set
{
mid_name = value;
}
}
public string LastName
{
get
{
return last_name;
}
set
{
last_name = value;
}
}
public short Age
{
get
{
return p_age;
}
set
{
p_age = value;
}
}
public char? Sex
{
get
{
return p_sex;
}
set
{
p_sex = value;
}
}
}
}
Now go to the Program.cs or Module.vb and write the following code to add Person objects to the List(T) collection:
static void Main(string[] args)
{
ListpList = new List ();
pList.Add(new Person(1, "John", "", "Shields", 29, 'M'));
pList.Add(new Person(2, "Mary", "Matthew", "Jacobs", 35, 'F'));
pList.Add(new Person(3, "Amber", "Carl", "Agar", 25, 'M'));
pList.Add(new Person(4, "Kathy", "", "Berry", 21, 'F'));
pList.Add(new Person(5, "Lena", "Ashco", "Bilton", 33, 'F'));
pList.Add(new Person(6, "Susanne", "", "Buck", 45, 'F'));
pList.Add(new Person(7, "Jim", "", "Brown", 38, 'M'));
pList.Add(new Person(8, "Jane", "G", "Hooks", 32, 'F'));
pList.Add(new Person(9, "Robert", "", "", 31, 'M'));
pList.Add(new Person(10, "Cindy", "Preston", "Fox", 25, 'F'));
pList.Add(new Person(11, "Gina", "", "Austin", 27, 'F'));
pList.Add(new Person(12, "Joel", "David", "Benson", 33, 'M'));
pList.Add(new Person(13, "George", "R", "Douglas", 55, 'M'));
pList.Add(new Person(14, "Richard", "", "Banks", 22, 'M'));
pList.Add(new Person(15, "Mary", "C", "Shaw", 39, 'F'));
}
I have also created a common method ‘PrintOnConsole’ that will help us print the List(T) on the console:
static void PrintOnConsole(ListpList, string info)
{
Console.WriteLine(info);
Console.WriteLine("\n{0,2} {1,7} {2,8} {3,8} {4,2} {5,3}",
"ID", "FName", "MName", "LName", "Age", "Sex");
pList.ForEach(delegate(Person per)
{
Console.WriteLine("{0,2} {1,7} {2,8} {3,8} {4,2} {5,3}",
per.ID, per.FirstName, per.MiddleName, per.LastName, per.Age, per.Sex);
});
Console.ReadLine();
}
1. Looping through all items in the List(T)
PrintOnConsole(pList, "1. --- Looping through all items in the List---");
2. Filtering List(T) using a single condition - (Age > 35)
ListfilterOne = pList.FindAll(delegate(Person p) { return p.Age > 35; });
PrintOnConsole(filterOne, "2. --- Filtering Liston single condition (Age > 35) ---");
3. Filtering List(T) on multiple conditions (Age > 35 and Sex is Female)
ListfilterMultiple = pList.FindAll(delegate(Person p) { return p.Age > 35 && p.Sex == 'F'; });
PrintOnConsole(filterMultiple, "3. --- Filtering Liston multiple conditions (Age > 35 and Sex is Female) ---");
4. Sorting List(T) (Sort on FirstName)
ListsortFName = pList;
sortFName.Sort(delegate(Person p1, Person p2)
{
return p1.FirstName.CompareTo(p2.FirstName);
});
PrintOnConsole(sortFName, "4. --- Sort List(Sort on FirstName) ---");
<
5. Sorting List(T) descending (Sort on LastName descending)
ListsortLNameDesc = pList;
sortLNameDesc.Sort(delegate(Person p1, Person p2)
{
return p2.LastName.CompareTo(p1.LastName);
});
PrintOnConsole(sortLNameDesc, "5. --- Sort Listdescending (Sort on LastName descending) ---");
6. Add new List(T) to existing List(T)
ListnewList = new List ();
newList.Add(new Person(16, "Geoff", "", "Fisher", 29, 'M'));
newList.Add(new Person(17, "Samantha", "Carl", "Baxer", 32, 'F'));
pList.AddRange(newList);
PrintOnConsole(pList, "6. --- Add new Listto existing List<> ---");
7. Remove multiple items from List(T) based on condition (remove male employees)
ListremoveList = pList;
removeList.RemoveAll(delegate(Person p) { return p.Sex == 'M'; });
PrintOnConsole(removeList, "7. --- Remove multiple items from List<> based on condition ---");
8. Create Read Only List(T)
Console.WriteLine("8. --- Create Read Only List<> ---");
IListpersonReadOnly = pList;
Console.WriteLine("Before - Is List Read Only? True or False : " + personReadOnly.IsReadOnly);
personReadOnly = pList.AsReadOnly();
Console.WriteLine("After - Is List Read Only? True or False : " + personReadOnly.IsReadOnly);
Console.ReadLine();
Read more...