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
Removing the specified element from the List in C#
The List<T>.Remove() method in C# removes the first occurrence of a specified element from the list. It returns true if the element was successfully removed, or false if the element was not found in the list.
Syntax
Following is the syntax for the Remove() method −
public bool Remove(T item)
Parameters
- item − The element to remove from the list
Return Value
Returns true if the element is successfully found and removed; otherwise, false.
Using Remove() with String Lists
The following example demonstrates removing a string element from a list −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<String> countries = new List<String>();
countries.Add("India");
countries.Add("US");
countries.Add("UK");
countries.Add("Canada");
countries.Add("Poland");
countries.Add("Netherlands");
Console.WriteLine("Original List:");
foreach (string country in countries) {
Console.WriteLine(country);
}
Console.WriteLine("\nCount before removal: " + countries.Count);
bool removed = countries.Remove("US");
Console.WriteLine("Remove 'US': " + removed);
Console.WriteLine("\nCount after removal: " + countries.Count);
Console.WriteLine("Updated List:");
foreach (string country in countries) {
Console.WriteLine(country);
}
}
}
The output of the above code is −
Original List: India US UK Canada Poland Netherlands Count before removal: 6 Remove 'US': True Count after removal: 5 Updated List: India UK Canada Poland Netherlands
Using Remove() with Integer Lists
The following example shows removing an integer element from a list −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> numbers = new List<int>();
numbers.Add(5);
numbers.Add(10);
numbers.Add(20);
numbers.Add(50);
numbers.Add(75);
numbers.Add(100);
Console.WriteLine("Original List:");
foreach (int num in numbers) {
Console.WriteLine(num);
}
Console.WriteLine("\nCount before removal: " + numbers.Count);
bool removed = numbers.Remove(50);
Console.WriteLine("Remove '50': " + removed);
Console.WriteLine("\nCount after removal: " + numbers.Count);
Console.WriteLine("Updated List:");
foreach (int num in numbers) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Original List: 5 10 20 50 75 100 Count before removal: 6 Remove '50': True Count after removal: 5 Updated List: 5 10 20 75 100
Handling Non-Existent Elements
When attempting to remove an element that doesn't exist in the list, the method returns false −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> fruits = new List<string> {"Apple", "Banana", "Orange"};
Console.WriteLine("Original List:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
bool result1 = fruits.Remove("Banana");
bool result2 = fruits.Remove("Mango");
Console.WriteLine("\nRemove 'Banana': " + result1);
Console.WriteLine("Remove 'Mango': " + result2);
Console.WriteLine("\nFinal List:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Original List: Apple Banana Orange Remove 'Banana': True Remove 'Mango': False Final List: Apple Orange
Key Points
-
Remove()only removes the first occurrence of the specified element - The method returns a
boolvalue indicating success or failure - If the element doesn't exist, no exception is thrown − it simply returns
false - After removal, all subsequent elements shift one position to the left
Conclusion
The List<T>.Remove() method provides an efficient way to remove specific elements from a list. It returns a boolean value indicating whether the removal was successful, making it easy to handle cases where the element might not exist in the list.
