Add Some Elegance To Your Code Using C# List

A short and to-the-point tutorial that demonstrates how to sort and search using List in C#.

It is a fairly common programming scenario to find ourselves with a list of identical objects. In the past, without adequate support from programming languages, we found ourselves writing a lot of searching and sorting code, and that may have put you off using lists in favor of arrays. All that has changed with C# (particularly 2.0) - its implementation of a list makes handling such lists remarkably easy.

For example, given the following class Person: 
  1. public class Person  
  2. {  
  3.           public int age;  
  4.           public string name;  
  5.           public Person(int age, string name)  
  6.           {  
  7.                    this.age = age;  
  8.                    this.name = name;  
  9.           }  
  10. }  
We can create a list of Person objects and add six people like so:
  1. List<person>people =new List<person>();   
  2.   
  3. people.Add(new Person(50, "Fred"));  
  4. people.Add(new Person(30, "John"));  
  5. people.Add(new Person(26, "Andrew"));  
  6. people.Add(new Person(24, "Xavier"));  
  7. people.Add(new Person(5, "Mark"));  
  8. people.Add(new Person(6, "Cameron"));   
C#'s list mechanism provides us with a number of useful methods. Personally, I find ForEach, FindAll and Sort to be very useful. ForEach allows us access to each item in the list. FindAll allows us to search for objects in the list that match a specific condition. Sort allows us to sort the objects in the list. The following code demonstrates how we might use each of these methods:
  1. Console.WriteLine("Unsorted list");  
  2.  Console.WriteLine("--------------");  
  3.  people.ForEach(delegate (Person p)  
  4.  { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });  
  5.   
  6.  // Find the young     
  7.  List<Person> young = people.FindAll(delegate (Person p) { return p.age < 25; });  
  8.  Console.WriteLine("Age is less than 25");  
  9.  Console.WriteLine("-------------------");  
  10.  young.ForEach(delegate (Person p)  
  11.  { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });  
  12.   
  13.   
  14.  // Sort by name    
  15.  Console.WriteLine("Sorted list, by name");  
  16.  Console.WriteLine("---------------------");  
  17.  people.Sort(delegate (Person p1, Person p2)  
  18.  { return p1.name.CompareTo(p2.name); });  
  19.   
  20.      people.ForEach(delegate (Person p)  
  21.      {  
  22.          Console.WriteLine(String.Format("{0} {1}", p.age, p.name));  
  23.      });  
  24.   
  25.   
  26.      // Sort by age    
  27.      Console.WriteLine("Sorted list, by age");  
  28.      Console.WriteLine("-------------------");  
  29.      people.Sort(delegate (Person p1, Person p2)  
  30.      {  
  31.          return p1.age.CompareTo(p2.age);  
  32.      });  
  33.   
  34.      people.ForEach(delegate (Person p)  
  35.      {  
  36.          Console.WriteLine(String.Format("{0} {1}", p.age, p.name));  
  37.      });  
And here is the output that we should expect:
 
C# list 

Lists are powerful and result in fewer, and more elegant, lines of code. Hopefully, this short example has demonstrated their ease and you will find yourself using them in your day-to-day development activities.

More related articles