Overview Of List Collection Class In C#

In this article, you will learn about List collection class in C#.

Introduction
  • List<T> class represents a strongly typed list of objects that can be accessed by index.
  • List<T > is one of the generic collection classes; it is present in System.Collections.Generic namespace
  • It can be used to create a collection of any type.
  • It is a generic equivalent of ArrayList class, but the additional features it has is that List<T> class is type safe and gives better performance.
  • A reference type can also be used in place of type T.
  • List<T> class defines a list of generic methods for performing various operations on the list like search, sort, and manipulate list.
  • List<T> accepts a null reference as a valid value for reference types and allows duplicate elements.
Explanation

Before beginning the explanation, please read and understand the collection class in .NET.

Example for the List<T> class is as follows,

 

As you can see in the above example, I have declared and initialized a list of strings in _days variable and I am adding items in the list using Add() method. Using foreach loop, I am displaying all the elements in the list.

Here, you can add elements any time, at any index position, no matter the list size. For example, if you wanted to add “Wednesday” at third position, then you can use Insert(index, item) method. Here, the third position,i.e., list is zero index based so third position=3-1=2, as shown below.

 

I have explained about the List of string items. Here, it's not only string but you can have any objects like int, double, class, etc.

Let’s see for the list of class items, as shown below.

 

In the above image, you can see that I have marked 2 in a red rectangle that is the initial size of list, but you can see here also that I have added 3 items, which are out of range. If it is an array, then it will throw an out of range exception as shown below.

 

There are some useful methods in List<T> class object, as shown below.

  1. Exists() function
    This function is used to check whether the item exists in the list based on the condition. This function returns true if the item exists, else it returns false.

  2. Contains() function
    This function is used to check whether the item exists in the list. This function returns true if the item exists else false.

  3. Find() function
    It searches for an element that matches the conditions defined by the specified lambda expression and returns the first matching item from the list.

  4. FindLast() function
    It searches for an element that matches the conditions defined by the specified lambda expression and returns the last matching item from the list.

  5. FindAll() function
    Returns all the items from the list that match the conditions defined by the specified lambda expression.

  6. FindIndex() function
    It returns the index of the first item that matches the condition specified by the lambda expression. There are 2 other overloads of this function which allow us to specify the range of elements to search, within the list.

  7. FindLastIndex() function
    It returns the index of the last item that matches the condition specified by the lambda expression. There are 2 other overloads of this function which allow us to specify the range of elements to search, within the list.

  8. GetRange() function
    Using an item index, we can retrieve only one item at a time from the list. If you want to get a list of items from the list, then use this function. This function expects two parameters, i.e. the start index and the number of elements to return.

  9. Add() function
    It allows you to add one item at a time, at the end of the list.

  10. AddRange() function
    It allows you to add another list of items at the end of the list.

  11. Insert() function
    It allows you to insert only one item in the list at specified index.

  12. InsertRange() function
    It allows you to insert another list of item to the list at the specified index.

  13. Remove() function
    It is used to remove only first matching item from list.

  14. RemoveAt() function
    It is used to remove an item at specified index in the list.

  15. RemoveRange() function
    It is used remove range of items from the list, it expects two parameters, i.e., the start index and the number of elements to remove.

  16. RemoveAll() function
    It is used to remove all the items from the list that match the specified condition.

  17. Clear() function
    It is used to remove all the items without specifying condition.

  18. ToArray() function
    Convert a list to an Array.

  19. ToDictionary() function
    Convert a list to a Dictionary.

  20. Sort() function
    It is used sto ort numeric values or strings in ascending order.

  21. Reverse() function
    It is used to sort numeric values or strings in descending order.