Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# LinkedList Class
Next article icon

C# List Class

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.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.

  • 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 equal to Capacity, then the capacity of the List increases 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 the ArrayList class by implementing the IList<T> generic interface.
  • This class can use both equality and ordering comparisons.
  • List<T> class is not sorted by default and elements are accessed by a 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: This example demonstrates how to create a list, add elements to it, and then display the elements using the for-each loop.

C#
// C# program to add elements to a List using System; using System.Collections.Generic;  class Geeks {     static void Main()     {         // Create a new list of integers         List<int> l = new List<int>();          // Add elements to the list         l.Add(10);         l.Add(20);         l.Add(30);         l.Add(40);          // Display the elements in the list         Console.WriteLine("Elements in the list:");         foreach (int i in l)         {             Console.WriteLine(i);         }     } } 

Output
Elements in the list: 10 20 30 40 

Declaration of List

List<T> listName = new List<T>();

Constructors

ConstructorsDescription
List<T>()Initializes a new instance of the List<T> class that is empty and has the default initial capacity.
List<T>(IEnumerable<T>)Initializes a new instance of the List<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
List<T>(Int32)Initializes a new instance of the List<T> class that is empty and has the specified initial capacity

Example: This example demonstrates how to create an empty List and display its initial count.

C#
// C# program to demonstrates the count of an emptyList using System; using System.Collections.Generic;  class Geeks {      public static void Main(String[] args)     {          // Creating a List of integers         List<int> l = new List<int>();          // displaying the number         // of elements of List<T>         Console.WriteLine(l.Count);     } } 

Output
0 

Properties

The List class provides several properties to access its state.

PropertiesDescription
CapacityGets or sets the total number of elements the internal data structure can hold without resizing.
CountGets the number of elements contained in the List<T>.
Item[Int32]Gets or sets the element at the specified index.

Example: This example, demonstrates the list capacity and count.

C#
// C# Program to demonstrates the  // properties how count and capacity work using System; using System.Collections.Generic;  class Geeks {      public static void Main(String[] args)     {         // Creating a List of integers         List<int> l = new List<int>();          // Adding elements to the List one by one         l.Add(1);         l.Add(2);         l.Add(3);         l.Add(4);         l.Add(5);         l.Add(6);          // Printing the Capacity and Count of l         Console.WriteLine("Capacity: " + l.Capacity);         Console.WriteLine("Count: " + l.Count);     } } 

Output
Capacity: 8 Count: 6 

Methods

MethodDescription
Add(T)Adds an object to the end of the List<T>.
AddRange(IEnumerable<T>)Adds the elements of the specified collection to the end of the List<T>.
AsReadOnly()Returns a read-only ReadOnlyCollection<T> wrapper for the current collection.
BinarySearch()Uses a binary search algorithm to locate a specific element in the sorted List<T> or a portion of it.
Clear()Removes all elements from the List<T>.
Contains(T)Determines whether an element is in the List<T>.
ConvertAll(Converter)Converts the elements in the current List<T> to another type, and returns a list containing the converted elements.
CopyTo()Copies the List<T> or a portion of it to an array.
Equals(Object)Determines whether the specified object is equal to the current object.
Exists(Predicate<T>)Determines whether the List<T> contains elements that match the conditions defined by the specified predicate.
Find(Predicate<T>)Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T>.
FindAll(Predicate<T>)Retrieves all the elements that match the conditions defined by the specified predicate.
FindIndex()Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within the List<T> or a portion of it. This method returns -1 if an item that matches the conditions is not found.
FindLast(Predicate<T>)Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire List<T>.
FindLastIndex()Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within the List<T> or a portion of it.
ForEach(Action<T>)Performs the specified action on each element of the List<T>.
GetEnumerator()Returns an enumerator that iterates through the List<T>.
GetHashCode()Serves as the default hash function.
GetRange(Int32, Int32)Creates a shallow copy of a range of elements in the source List<T>.
GetType()Gets the Type of the current instance.
IndexOf()Returns the zero-based index of the first occurrence of a value in the List<T> or in a portion of it.
Insert(Int32, T)Inserts an element into the List<T> at the specified index.
InsertRange(Int32, IEnumerable<T>)Inserts the elements of a collection into the List<T> at the specified index.
LastIndexOf()Returns the zero-based index of the last occurrence of a value in the List<T> or in a portion of it.
MemberwiseClone()Creates a shallow copy of the current Object.
Remove(T)Removes the first occurrence of a specific object from the List<T>.
RemoveAll(Predicate<T>)Removes all the elements that match the conditions defined by the specified predicate.
RemoveAt(Int32)Removes the element at the specified index of the List<T>.
RemoveRange(Int32, Int32)Removes a range of elements from the List<T>.
Reverse()Reverses the order of the elements in the List<T> or a portion of it.
Sort()Sorts the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.
ToArray()Copies the elements of the List<T> to a new array.
ToString()Returns a string that represents the current object.
TrimExcess()Sets the capacity to the actual number of elements in the List<T>, if that number is less than a threshold value.
TrueForAll(Predicate<T>)Determines whether every element in the List<T> matches the conditions defined by the specified predicate.

Example 1: This example demonstrates how to check if a specified element is present in the list using Contains().

C#
// C# Program to check whether the // element is present in the List or not using System; using System.Collections.Generic;  class Geeks {      // Main Method     public static void Main(String[] args)     {          // Creating an List<T> of Integers         List<int> l = new List<int>();          // Adding elements to List         l.Add(1);         l.Add(2);         l.Add(3);          // Checking whether 2 is present         // in List or not         Console.Write("Element 2 is presnt in the List?: " + l.Contains(2));     } } 

Output
Element 2 is presnt in the List?: True

Example 2: This example printing the list before and after removing elements at specified index.

C#
// C# Program to remove the element at // the specified index of the List<T> using System; using System.Collections.Generic;  class Geeks {          public static void Main(String[] args)     {         // Creating a new List and          // adding elements to it         List<int> l = new List<int>();         l.Add(17);         l.Add(19);         l.Add(21);         l.Add(9);         l.Add(75);         l.Add(19);         l.Add(73);          // Printing the initial list         Console.WriteLine("Initial List: "                            + string.Join(", ", l));          // Removing elements from the list         l.RemoveAt(3);          l.Remove(19);            // Printing the updated list         Console.WriteLine("Updated List: "                            + string.Join(", ", l));     } } 

Output
Initial List: 17, 19, 21, 9, 75, 19, 73 Updated List: 17, 21, 75, 19, 73 


Next Article
C# LinkedList Class

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-Generic-List
  • CSharp-Generic-Namespace

Similar Reads

  • C# LinkedList Class
    LinkedList<T> class in C# is the part of the removal namespace. This generic type allows fast inserting and removal of elements. It implements a classic linked list. Each object is separately allocated. In the LinkedList, certain operations do not require the whole collection to be copied. But
    5 min read
  • C# SortedList Class
    SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic
    6 min read
  • C# Stack Class
    In C#, the Stack<T> class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it. The capa
    5 min read
  • C# SortedSet Class
    SortedSet class in C# represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. In C#, the SortedSet class can be used to store, remove, or view elements.It maintains ascending order and does not store duplicate elements.It is suggested to
    6 min read
  • C# LinkedList
    In C# a LinkedList is a linear data structure that stores elements in a non-contiguous location. The elements in a linked list are linked with each other using pointers. In other words, LinkedList consists of nodes where each node contains a data field and a reference(link) to the next node in the l
    5 min read
  • C# | Capacity of a List
    List<T>.Capacity Property is used to gets or sets the total number of elements the internal data structure can hold without resizing. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for refere
    4 min read
  • C# List Implementation
    In C#, a List is a generic collection used to store the elements or objects in the form of a list defined under System.Collection.Generic namespace. It provides the same functionality as ArrayList, the difference is a list is generic whereas ArrayList is a non-generic collection. It is dynamic means
    7 min read
  • C# ArrayList Class
    ArrayList class in C# is a part of the System.Collections namespace that represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching, and sorting items in the list. Elements ca
    8 min read
  • C# | How to create a SortedList
    SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. Properties of SortedList: Internally the object of SortedList maintains two arr
    2 min read
  • C# Data Structures
    Data Structures are an important part of programming language. When we are creating solutions for real-world problems, choosing the right data structure is critical because it can impact the performance of algorithms. If we know the fundamentals of data structures and know how to use them effectivel
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences