C# - List
The List is a collection of strongly typed objects that can be accessed by index and having methods for sorting, searching, and modifying list. It is the generic version of the ArrayList that comes under System.Collection.Generic namespace.
List Characteristics
- List equivalent of the ArrayList, which implements IList.
- It comes under System.Collections.Generic namespace.
- List can contain elements of the specified type. It provides compile-time type checking and doesn't perform boxing-unboxing because it is generic.
- Elements can be added using the Add[], AddRange[] methods or collection-initializer syntax.
- Elements can be accessed by passing an index e.g. myList[0]. Indexes start from zero.
- List performs faster and less error-prone than the ArrayList.
Creating a List
The List is a generic collection, so you need to specify a type parameter for the type of data it can store. The following example shows how to create list and add elements.
In the above example, List primeNumbers = new List[]; creates a list of int type. In the same way, cities and bigCities are string type list. You can then add elements in a list using the Add[] method or the collection-initializer syntax.
You can also add elements of the custom classes using the collection-initializer syntax. The following adds objects of the Student class in the List.
Adding an Array in a List
Use the AddRange[] method to add all the elements from an array or another collection to List.
AddRange[] signature: void AddRange[IEnumerable collection]
Accessing a List
A list can be accessed by an index, a for/foreach loop, and using LINQ queries. Indexes of a list start from zero. Pass an index in the square brackets to access individual list items, same as array. Use a foreach or for loop to iterate a List collection.
Accessing a List using LINQ
The List implements the IEnumerable interface. So, we can query a list using LINQ query syntax or method syntax, as shown below.
Insert Elements in List
Use the Insert[] method inserts an element into the List collection at the specified index.
Insert[] signature:void Insert[int index, T item];
Remove Elements from List
Use the Remove[] method to remove the first occurrence of the specified element in the List collection. Use the RemoveAt[] method to remove an element from the specified index. If no element at the specified index, then the ArgumentOutOfRangeException will be thrown.
Remove[] signature:bool Remove[T item]
RemoveAt[] signature: void RemoveAt[int index]
Check Elements in List
Use the Contains[] method to determine whether an element is in the List or not.
List Class Hierarchy
The following diagram illustrates the List hierarchy.
List Class Properties and Methods
The following table lists the important properties and methods of List class:
Items | Gets or sets the element at the specified index |
Count | Returns the total number of elements exists in the List |
Add | Adds an element at the end of a List. |
AddRange | Adds elements of the specified collection at the end of a List. |
BinarySearch | Search the element and returns an index of the element. |
Clear | Removes all the elements from a List. |
Contains | Checks whether the specified element exists or not in a List. |
Find | Finds the first element based on the specified predicate function. |
Foreach | Iterates through a List. |
Insert | Inserts an element at the specified index in a List. |
InsertRange | Inserts elements of another collection at the specified index. |
Remove | Removes the first occurrence of the specified element. |
RemoveAt | Removes the element at the specified index. |
RemoveRange | Removes all the elements that match the supplied predicate function. |
Sort | Sorts all the elements. |
TrimExcess | Sets the capacity to the actual number of elements. |
TrueForAll | Determines whether every element in theList matches the conditions defined by the specified predicate. |