Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove items from a list in C#?
In C#, there are several ways to remove items from a List<T>. The most commonly used methods are Remove(), RemoveAt(), RemoveAll(), and Clear(). Each method serves different purposes depending on whether you want to remove by value, by index, by condition, or all items.
Syntax
Following are the common syntaxes for removing items from a list −
// Remove by value list.Remove(item); // Remove by index list.RemoveAt(index); // Remove all items matching a condition list.RemoveAll(predicate); // Remove all items list.Clear();
Using Remove() Method
The Remove() method removes the first occurrence of a specific item from the list. It returns true if the item was found and removed, otherwise false −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> myList = new List<string>();
myList.Add("Jennings");
myList.Add("James");
myList.Add("Chris");
Console.WriteLine("Initial List...");
foreach(string str in myList) {
Console.WriteLine(str);
}
bool removed = myList.Remove("James");
Console.WriteLine("Item removed: " + removed);
Console.WriteLine("New List...");
foreach(string str in myList) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
Initial List... Jennings James Chris Item removed: True New List... Jennings Chris
Using RemoveAt() Method
The RemoveAt() method removes an item at a specific index position. Remember that list indices start from 0 −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> {10, 20, 30, 40, 50};
Console.WriteLine("Initial List:");
foreach(int num in numbers) {
Console.Write(num + " ");
}
numbers.RemoveAt(2); // Remove item at index 2 (value 30)
Console.WriteLine("\nAfter removing item at index 2:");
foreach(int num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Initial List: 10 20 30 40 50 After removing item at index 2: 10 20 40 50
Using RemoveAll() Method
The RemoveAll() method removes all items that match a specified condition using a predicate function −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> {1, 15, 8, 25, 12, 30};
Console.WriteLine("Initial List:");
foreach(int num in numbers) {
Console.Write(num + " ");
}
int removed = numbers.RemoveAll(x => x > 15);
Console.WriteLine("\nRemoved " + removed + " items greater than 15:");
foreach(int num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Initial List: 1 15 8 25 12 30 Removed 2 items greater than 15: 1 15 8 12
Using Clear() Method
The Clear() method removes all items from the list −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> colors = new List<string> {"Red", "Green", "Blue"};
Console.WriteLine("Count before Clear(): " + colors.Count);
colors.Clear();
Console.WriteLine("Count after Clear(): " + colors.Count);
}
}
The output of the above code is −
Count before Clear(): 3 Count after Clear(): 0
Comparison of Removal Methods
| Method | Purpose | Return Value |
|---|---|---|
| Remove(item) | Removes first occurrence of specific item | bool (true if removed) |
| RemoveAt(index) | Removes item at specific index | void |
| RemoveAll(predicate) | Removes all items matching condition | int (count removed) |
| Clear() | Removes all items | void |
Conclusion
C# provides multiple methods to remove items from a List: Remove() for specific values, RemoveAt() for index-based removal, RemoveAll() for conditional removal, and Clear() to empty the entire list. Choose the appropriate method based on whether you know the item value, index, or need to remove multiple items at once.
