Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. List<T>.Add() / Remove() / Clear()

List<T>.Add() / Remove() / Clear()

Since: C# 2.0(2005)

Methods for working with a variable-length list: Add() to add an element, Remove() to remove an element, and Clear() to remove all elements.

Syntax

using System.Collections.Generic;

// Create a List<T>.
List<T> list = new List<T>();

// Add an element to the end of the list.
list.Add(T item)

list.Remove(T item)

list.RemoveAt(int index)

// Remove all elements from the list.
list.Clear()

Method List

MethodDescription
Add(T item)Adds an element to the end of the list.
AddRange(IEnumerable<T> collection)Adds all elements of the specified collection to the end of the list.
Remove(T item)Removes the first element that matches the specified value. Returns true if an element was removed.
RemoveAt(int index)Removes the element at the specified index.
Clear()Removes all elements from the list. Count becomes 0.

Sample Code

Use Add() to add elements, and Remove() / RemoveAt() to remove them.

Program.cs
using System;
using System.Collections.Generic;

List<string> members = new List<string>();
members.Add("Gojo Satoru");
members.Add("Itadori Yuji");
members.Add("Fushiguro Megumi");
Console.WriteLine(string.Join(", ", members));

string[] additional = { "Kugisaki Nobara", "Ryomen Sukuna" };
members.AddRange(additional);
Console.WriteLine(string.Join(", ", members));

members.Remove("Itadori Yuji");
Console.WriteLine(string.Join(", ", members));

members.RemoveAt(0);
Console.WriteLine(string.Join(", ", members));

members.Clear();
Console.WriteLine(members.Count);

This produces the following output:

dotnet run
Gojo Satoru, Itadori Yuji, Fushiguro Megumi
Gojo Satoru, Itadori Yuji, Fushiguro Megumi, Kugisaki Nobara, Ryomen Sukuna
Gojo Satoru, Fushiguro Megumi, Kugisaki Nobara, Ryomen Sukuna
Fushiguro Megumi, Kugisaki Nobara, Ryomen Sukuna
0

Removing All Matching Elements with RemoveAll()

Remove() only deletes the first matching element. To remove all elements that match a condition, use RemoveAll(). Pass a lambda expression specifying the condition.

RemoveAllSample.cs
using System;
using System.Collections.Generic;

List<int> scores = new List<int> { 85, 40, 92, 35, 78, 50, 61 };

// Remove all elements below 50
int removed = scores.RemoveAll(s => s < 50);
Console.WriteLine($"Removed count: {removed}");
Console.WriteLine(string.Join(", ", scores));

This produces the following output:

dotnet run
Removed count: 2
85, 92, 78, 50, 61

Practical Pattern: Building a List Dynamically

List<T> is commonly used to collect data dynamically when the number of elements is not known in advance. After processing, use ToArray() or string.Join() to output the result.

BuildList.cs
using System;
using System.Collections.Generic;

// Collect even numbers from 1 to 20
List<int> evens = new List<int>();
for (int i = 1; i <= 20; i++)
{
    if (i % 2 == 0)
    {
        evens.Add(i);
    }
}
Console.WriteLine($"Even count: {evens.Count}");
Console.WriteLine(string.Join(", ", evens));

// Convert to an array with ToArray()
int[] arr = evens.ToArray();
Console.WriteLine(arr.GetType().Name); // Int32[]

This produces the following output:

dotnet run
Even count: 10
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
Int32[]

Common Mistakes

Common Mistake: Remove() Only Deletes the First Occurrence

When the same value appears multiple times, Remove() only deletes the first occurrence. Use RemoveAll() if you want to remove all matching elements.

using System;
using System.Collections.Generic;

List<string> names = new List<string> { "Itadori Yuji", "Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi" };

// NG: Remove() only deletes the first occurrence
names.Remove("Itadori Yuji");
Console.WriteLine(string.Join(", ", names)); // Gojo Satoru, Itadori Yuji, Fushiguro Megumi (still remains)

This produces the following output:

dotnet run
Gojo Satoru, Itadori Yuji, Fushiguro Megumi
using System;
using System.Collections.Generic;

// OK: Use RemoveAll() to delete all occurrences
List<string> names = new List<string> { "Itadori Yuji", "Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi" };
names.RemoveAll(n => n == "Itadori Yuji");
Console.WriteLine(string.Join(", ", names)); // Gojo Satoru, Fushiguro Megumi

This produces the following output:

dotnet run
Gojo Satoru, Fushiguro Megumi

Notes

List<T> is the most commonly used collection class in C#. Internally it is backed by an array, so appending to the end with Add() is fast. However, inserting or removing elements in the middle requires shifting subsequent elements, so be mindful of performance when the list is large.

Remove() deletes only the first matching element. If the same value appears multiple times and you want to remove all occurrences, use RemoveAll() instead.

For inserting elements and retrieving the element count, see 'List<T>.Insert() / Count'.

If you find any errors or copyright issues, please .