How to use Generics in C#
Suppose we have a simple class People
Code:
protected class People : IComparable
{
protected string name = String.Empty;
protected int age = 0;
protected double salary = 0.0d;
/// <summary>
/// The constructor of the class People
/// </summary>
/// <param name="n"></param>
/// <param name="a"></param>
/// <param name="s"></param>
public People(string n, int a, double s)
{
name = n;
age = a;
salary = s;
}
/// <summary>
/// Override the ToString() method
/// </summary>
/// <returns></returns>
override public string ToString()
{
return String.Format("{0:s}, {1:G} - Salary: {2:c}",name, age, salary);
}
/// <summary>
/// Override the CompareTo method,
/// compare name fields
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo(object obj)
{
People otherPeople = obj as People;
if (otherPeople != null)
return this.name.CompareTo(otherPeople.name);
else
throw new ArgumentException("Object is not a People");
}
}
We want to insert objects of type People in a list, and then manipulating them.
Code:
List<People> people = new List<People>();
people.Add(new People("Yasmine", 32, 1800.00d));
people.Add(new People("Gabrielle", 38, 2300.00d));
people.Add(new People("Lynette", 35, 2200.00d));
people.Add(new People("Asia", 33, 1900.00d));
people.Add(new People("Daisy", 36, 2000.00d));
people.Add(new People("Lorelayne", 39, 2100.00d));
people.Add(new People("Jenny", 36, 2300.00d));
people.Add(new People("Lisa", 34, 2600.00d));
people.Add(new People("Ylenia", 37, 1700.00d));
Response.Write("<h2>List of people (not sorted)</h2>");
foreach(People p in people)
{
Response.Write(p.ToString() + "<br />");
}
// Sort the List, the CompareTo method, of the class People, determines which field will be performed comparing
people.Sort();
Response.Write("<h2>List of people (sorted by name ASC)</h2>");
foreach (People p in people)
{
Response.Write(p.ToString() + "<br />");
}
// Reverses the order of the elements of the list
people.Reverse();
Response.Write("<h2>List of people (sorted by name DESC)</h2>");
foreach (People p in people)
{
Response.Write(p.ToString() + "<br />");
}
Response.Write("<h3>Some information of the list</h3>");
Response.Write("<code>people.Capacity</code>: " + people.Capacity + "<br />");
Response.Write("<code>people.Count</code>: " + people.Count + "<br />");
Here is the result for the sample:
List of people (not sorted)
Yasmine, 32 - Salary: $1,800.00
Gabrielle, 38 - Salary: $2,300.00
Lynette, 35 - Salary: $2,200.00
Asia, 33 - Salary: $1,900.00
Daisy, 36 - Salary: $2,000.00
Lorelayne, 39 - Salary: $2,100.00
Jenny, 36 - Salary: $2,300.00
Lisa, 34 - Salary: $2,600.00
Ylenia, 37 - Salary: $1,700.00
List of people (sorted by name ASC)
Asia, 33 - Salary: $1,900.00
Daisy, 36 - Salary: $2,000.00
Gabrielle, 38 - Salary: $2,300.00
Jenny, 36 - Salary: $2,300.00
Lisa, 34 - Salary: $2,600.00
Lorelayne, 39 - Salary: $2,100.00
Lynette, 35 - Salary: $2,200.00
Yasmine, 32 - Salary: $1,800.00
Ylenia, 37 - Salary: $1,700.00
List of people (sorted by name DESC)
Ylenia, 37 - Salary: $1,700.00
Yasmine, 32 - Salary: $1,800.00
Lynette, 35 - Salary: $2,200.00
Lorelayne, 39 - Salary: $2,100.00
Lisa, 34 - Salary: $2,600.00
Jenny, 36 - Salary: $2,300.00
Gabrielle, 38 - Salary: $2,300.00
Daisy, 36 - Salary: $2,000.00
Asia, 33 - Salary: $1,900.00
Some information of the list
people.Capacity
: 16
people.Count
: 9