In this tutorial, we will discuss the functionality of the List<T> with examples in C#.
What is C# List <T> Collection?
In C#, the List<T> collection defines a dynamic array and a generic collection. It lets users keep a collection of elements of similar type and gives ways for adding, deleting, and modifying those elements. In the .NET framework, List<T> is a component of the “System.Collections.Generic” namespace.
Syntax
The general syntax of the List<T> collection in C# is provided below:
Replace “T” with the appropriate list type and “a, b, c,..” all the first items will be inserted into the list in this manner. Here, the keyword “new” is applied for creating a new instance for the List<T> class.
Example 1: Adding Elements to the List <string>
Let’s look at a generic List<T> class that saves entries using the “Add()” method and iterates the list with the for-each loop:
using System.Collections.Generic;
class Program {
public static void Main(String[] args)
{
List<string> students = new List<string>(5);//creation of list
students.Add("Amna");//adding of elements
students.Add("Kaynat");
students.Add("Ahmad");
Console.WriteLine("Student Names");
Console.WriteLine("*************");
foreach (string a in students)//printing items in the student list
Console.WriteLine(a);
Console.WriteLine("*************");
}
};
In the above code, generates a new list of string objects called “students” and adds three strings into it, then prints each item using a “foreach” loop. The output of the above-provided code is as follows:
Characteristics
- It is not the same as arrays, List<T> may be dynamically enlarged, whereas arrays cannot.
- The List<T> class supports duplicate elements and accepts null as a valid value for any reference type.
- The internal array will be reallocated to expand the List’s capacity if the count rises to the level of capacity before the new element is added, the current element will be copied into the new array.
- List<T> is implemented by the List<T> equivalents of the arrayList.
- The elements of a List<T> class are accessed using a zero-based index instead of by default sorted.
- On a 64-bit system, users may increase the allowed capacity for very big List<T> objects to 2 billion entries by setting the enabled property in the configuration element set true.
Properties of List<T>
Capacity: The internal data structure’s maximum number of elements that may be held without resizing is obtained or adjusted using the capacity property.
Count: Determines how many elements are there in the List<T>.
Item[Int32]: The element on the provided index is retrieved or set.
Example 2: List<T> Count and Capacity Property
The following C# program demonstrates the count and capacity properties of List<T>:
using System.Collections.Generic;
class Program {
public static void Main(String[] args)
{
List<int> intlist = new List<int>();
intlist.Add(1);
intlist.Add(2);
intlist.Add(3);
intlist.Add(4);
intlist.Add(5);
intlist.Add(6);
Console.WriteLine("list capacity: " + intlist.Capacity);
Console.WriteLine("list count: " + intlist.Count);
intlist.Add(7);
intlist.Add(8);
intlist.Add(9);
intlist.Add(10);
Console.WriteLine("Capacity after inserting new elements: " + intlist.Capacity);
Console.WriteLine("Count after inserting new elements: " + intlist.Count);
}
}
In the above code, first, we created a class “Program” and then initialized a list of integers with the help of List<int> intlist. After creating the list, we added six elements in the list by calling the “Add()” function and then printing the capacity and the count on the console. After that, inserted four new elements to the list using the “intlist.Add()”. Next, print the updated count and capacity. As follows:
List <T > Methods
In C#, the List<T> class provides several helpful methods for handling lists. Here, we have listed their most common methods:
Add: Inserts an element to the very end of a List<T>.
AddRange: Inserts the specified collection’s elements into the List’s end.
Binary Search: Applies a binary search strategy to find a specific element within the sorted List<T>.
Clear: Deletes all elements of a List<T>.
For Each: Iterate over a List<T>.
Insert: Adds an element in a List<T> at the specified index.
Insert Range: Adds elements from another collection into the specified index.
Remove Range: All entries that meet the specified predicate function are removed by this method.
Sort: Sorts every element.
Trim Excess: Sets the capacity by the exact amount of List<T> items.
True for all: Checks whether each element in the List<T> meets the requirements stated by the specified predicate.
Conclusiaon
The C# List<T> collection is an adaptable technique of storing a dynamic array containing objects of a given type “T”. It has several ways for manipulating and accessing list components, which makes it an effective tool for data storage as well as manipulation for C#. This tutorial illustrated the C# List<T> collection.