C# | List Class - GeeksforGeeks

03 Apr, 2019
List<T> class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List<T> class also provides the methods to search, sort, and manipulate lists.
Characteristics:
It is different from the arrays. A List<T> can be resized dynamically but arrays cannot.
List<T> class can accept null as a valid value for reference types and it also allows duplicate elements.
If the Count becomes equals to Capacity, then the capacity of the List increased automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
List<T> class is the generic equivalent of ArrayList class by implementing the IList<T> generic interface.
This class can use both equality and ordering comparer.
List<T> class is not sorted by default and elements are accessed by zero-based index.
For very large List<T> objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the configuration element to true in the run-time environment.
Example:
// C# program to create a List<T>using System;using System.Collections.Generic;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // Creating a List of integers        List<int> firstlist = new List<int>();          // displaying the number        // of elements of List<T>        Console.WriteLine(firstlist.Count);    }}
Output:
0

Example:
// C# program to illustrate the// Capacity Property of List<T>using System;using System.Collections.Generic;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // Creating a List of integers        // Here we are not setting        // Capacity explicitly        List<int> firstlist = new List<int>();          // adding elements in firstlist        firstlist.Add(1);        firstlist.Add(2);        firstlist.Add(3);        firstlist.Add(4);          // Printing the Capacity of firstlist        Console.WriteLine("Capacity Is: " + firstlist.Capacity);          // Printing the Count of firstlist        Console.WriteLine("Count Is: " + firstlist.Count);          // Adding some more        // elements in firstlist        firstlist.Add(5);        firstlist.Add(6);          // Printing the Capacity of firstlist        // It will give output 8 as internally        // List is resized        Console.WriteLine("Capacity Is: " + firstlist.Capacity);          // Printing the Count of firstlist        Console.WriteLine("Count Is: " + firstlist.Count);    }}
Output:
Capacity Is: 4
Count Is: 4
Capacity Is: 8
Count Is: 6

Example 1:
// C# Program to check whether the// element is present in the List// or notusing System;using System.Collections.Generic;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // Creating an List<T> of Integers        List<int> firstlist = new List<int>();          // Adding elements to List        firstlist.Add(1);        firstlist.Add(2);        firstlist.Add(3);        firstlist.Add(4);        firstlist.Add(5);        firstlist.Add(6);        firstlist.Add(7);          // Checking whether 4 is present        // in List or not        Console.Write(firstlist.Contains(4));    }}
Output:
True

Example 2:
// C# Program to remove the element at// the specified index of the List<T>using System;using System.Collections.Generic;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // Creating an List<T> of Integers        List<int> firstlist = new List<int>();          // Adding elements to List        firstlist.Add(17);        firstlist.Add(19);        firstlist.Add(21);        firstlist.Add(9);        firstlist.Add(75);        firstlist.Add(19);        firstlist.Add(73);          Console.WriteLine("Elements Present in List:\n");          int p = 0;          // Displaying the elements of List        foreach(int k in firstlist)        {            Console.Write("At Position {0}: ", p);            Console.WriteLine(k);            p++;        }          Console.WriteLine(" ");          // removing the element at index 3        Console.WriteLine("Removing the element at index 3\n");          // 9 will remove from the List        // and 75 will come at index 3        firstlist.RemoveAt(3);          int p1 = 0;          // Displaying the elements of List        foreach(int n in firstlist)        {            Console.Write("At Position {0}: ", p1);            Console.WriteLine(n);            p1++;        }    }}
Output:
Elements Present in List:

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
 
Removing the element at index 3

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 75
At Position 4: 19
At Position 5: 73

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.7.2
CSharp-Generic-List
CSharp-Generic-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Class and Object
C# | Constructors
Introduction to .NET Framework
Extension Method in C#
C# | Delegates
C# | Abstract Classes
C# | Data Types
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
Common Language Runtime (CLR) in C#