codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

34 / 60 topics
33Collections in C#34List in C#35Dictionary in C#36Queue in C#37Stack in C#
Tutorials/C# Programming/List in C#
🔷C# Programming

List in C#

Updated 2026-05-15
10 min read

List in C#

Introduction

In C#, a List is a versatile and commonly used collection that represents an array whose size can dynamically grow or shrink as needed. It belongs to the System.Collections.Generic namespace, which provides type-safe collections. This tutorial will guide you through understanding how to work with lists in C#, including creating, adding elements, accessing elements, and performing various operations.

Concept

A List<T> is a generic collection that can hold elements of any data type specified by the type parameter T. Lists are part of the .NET Framework's collection classes and offer many useful methods for manipulating collections. Here are some key features of lists:

  • Dynamic Sizing: Lists automatically resize themselves when you add or remove items.
  • Type Safety: Lists enforce that all elements must be of the same type, enhancing type safety.
  • Index-Based Access: You can access elements using zero-based indexing.
  • Common Methods: Lists provide methods for adding, removing, searching, and sorting elements.

Examples

Creating a List

To create a list in C#, you need to include the System.Collections.Generic namespace. Here's how you can declare and initialize a list:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 // Creating a list of integers
9 List<int> numbers = new List<int>();
10
11 // Adding elements to the list
12 numbers.Add(1);
13 numbers.Add(2);
14 numbers.Add(3);
15
16 // Displaying the list
17 foreach (int number in numbers)
18 {
19 Console.WriteLine(number);
20 }
21 }
22}
Output
1
2
3

Accessing Elements

You can access elements in a list using their index. Remember that indices are zero-based:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
9
10 // Accessing elements by index
11 Console.WriteLine(names[0]); // Output: Alice
12 Console.WriteLine(names[1]); // Output: Bob
13 Console.WriteLine(names[2]); // Output: Charlie
14 }
15}
Output
Alice
Bob
Charlie

Adding and Removing Elements

Lists provide methods to add and remove elements. Here are some common operations:

Adding Elements

You can add elements using the Add method or the AddRange method for adding multiple elements at once.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 List<int> numbers = new List<int>();
9
10 // Adding a single element
11 numbers.Add(10);
12
13 // Adding multiple elements using AddRange
14 numbers.AddRange(new int[] { 20, 30, 40 });
15
16 foreach (int number in numbers)
17 {
18 Console.WriteLine(number);
19 }
20 }
21}
Output
10
20
30
40

Removing Elements

You can remove elements using the Remove method to remove a specific element or the RemoveAt method to remove an element at a specific index.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
9
10 // Removing an element by value
11 fruits.Remove("Banana");
12
13 // Removing an element by index
14 fruits.RemoveAt(0);
15
16 foreach (string fruit in fruits)
17 {
18 Console.WriteLine(fruit);
19 }
20 }
21}
Output
Cherry

Searching and Sorting

Lists provide methods to search for elements and sort them.

Searching for an Element

You can use the Contains method to check if a list contains a specific element:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 List<string> colors = new List<string> { "Red", "Green", "Blue" };
9
10 // Checking if the list contains an element
11 bool hasGreen = colors.Contains("Green");
12 Console.WriteLine(hasGreen); // Output: True
13
14 bool hasYellow = colors.Contains("Yellow");
15 Console.WriteLine(hasYellow); // Output: False
16 }
17}
Output
True
False

Sorting Elements

You can sort the elements of a list using the Sort method:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 List<int> numbers = new List<int> { 5, 3, 8, 1, 2 };
9
10 // Sorting the list
11 numbers.Sort();
12
13 foreach (int number in numbers)
14 {
15 Console.WriteLine(number);
16 }
17 }
18}
Output
1
2
3
5
8

What's Next?

In the next section, we will explore another important collection type in C#: Dictionary. A dictionary allows you to store key-value pairs and provides efficient ways to access elements based on keys. Stay tuned!


PreviousCollections in C#Next Dictionary in C#

Recommended Gear

Collections in C#Dictionary in C#