Working with List in C#

Objective

When you have a number of objects that you want to manipulate, you can choose many type of methods to support containing and doing manipulation. One of the best choices is the List class. This tutorial will show you how to use List class in working with collection of objects.

Let's go

List

Although you can use array to do many things with collection of objects, but array has exposed an limitation, that is array doesn’t allow flexible extension. When you use an array, you must declare length of array before you can use. List comes across this problem and you can freely extend length of collections as you want. The default length of list is 16, but if you add more than 16 elements to the list, so it dynamically extends to adapt. You can also declare length of the list by using overload Constructor when you declare the list or by setting Capacity property before you use the list to increase the performance. When you declare a list, you also specify the type of object in the list. For example:

  1. List<int> lInt = new List<int>();

You can also specify length of list as follows:

  1. List<int> lInt = new List<int>(100);

After you have initialized an list, you can add element to list. Table bellow list some members that you can use:

NameDescription
Capacity Gets or sets the total number of elements the list can contain without resizing
CountGets the total number of elements actually contained in the list
AddAdds an object to the end of the list
AddRangeAdds a collection of objects to the end of the list
BinarySearch Searches a sorted list for a value using a binary search
Clear Removes all elements from the list
Contains Determines whether an element is in the list
Exists Determines whether the list contains elements that match the conditions specified
Find Searches for an element that matches the conditions defined and returns the first occurrence within the entire list
FindAll Retrieves all the elements that match the conditions defined
ForEach Performs the specified action on each element in the list
Sort Sorts the elements in the list
TrimExcess Sets the capacity to the actual number of elements in the list
TrueForAll Determines whether every element in the list matches the conditions defined

LinkedList

In case of you want to quickly insert/add elements to a list, you can use LinkedList. This class only allows sequentially access. For random access you should use List. An example of LinkListed is listed as follows:

  1. LinkedList<int> llInt = new LinkedList<int>();

Note that LinkedList don’t allow specifies length of list in Constructor member.

Working with List

To understanding the use of the list in C#, let see the example code bellows:

  1. //declare a list object
  2. List<int> lInt = new List<int>();
  3. //add elements to list
  4. lInt.Add(5);
  5. lInt.Add(10);
  6. lInt.Add(50);
  7. lInt.Add(70);
  8. //write number of elements in list
  9. Console.WriteLine("Length of the list " + lInt.Count);
  10. //remove 2nd element
  11. lInt.RemoveAt(2);
  12. //write number of elements in list
  13. Console.WriteLine("Length of the list " + lInt.Count);
The result of the program is: Length of the list 4
Length of the list 3

Explanation In this above code:

  • We simply declare an list object and add value (5,10,50,70) to the list by using Add member of list
  • We then get and write the length of list by using Count member of list
  • Lastly we remove the 2nd element in the list and then write length of list to check whether if we have removed successfully.

Summary

In this tutorial you leart of using List class in working with collection. Working with Collection in program is problem that you will get very usually, so you must try to investigate it carefully. Make sure you are choosing right way to do with collection based on the type of collection you are doing with, it’s the key to the success.

Add new comment