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 first occurrence of object from Collection in C#
The Collection<T>.Remove() method in C# removes the first occurrence of a specified object from the collection. This method searches through the collection sequentially and removes only the first matching element it encounters, leaving any subsequent duplicates unchanged.
Syntax
Following is the syntax for the Remove() method −
public bool Remove(T item)
Parameters
- item: The object to remove from the collection. The value can be null for reference types.
Return Value
The method returns a bool value −
- true: If the item was successfully found and removed
- false: If the item was not found in the collection
How It Works
The Remove() method uses the default equality comparer for type T to locate the item. It performs a linear search from the beginning of the collection and stops after removing the first match.
Example with String Collection
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<string> col = new Collection<string>();
col.Add("Andy");
col.Add("Kevin");
col.Add("John");
col.Add("Nathan");
col.Add("Nathan");
col.Add("Katie");
col.Add("Barry");
col.Add("Nathan");
col.Add("Mark");
Console.WriteLine("Count of elements = "+ col.Count);
Console.WriteLine("Original collection:");
foreach(string item in col) {
Console.WriteLine(item);
}
bool removed = col.Remove("Nathan");
Console.WriteLine("\nRemoval successful: " + removed);
Console.WriteLine("Count of elements (updated) = "+ col.Count);
Console.WriteLine("Updated collection:");
foreach(string item in col) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Count of elements = 9 Original collection: Andy Kevin John Nathan Nathan Katie Barry Nathan Mark Removal successful: True Count of elements (updated) = 8 Updated collection: Andy Kevin John Nathan Katie Barry Nathan Mark
Example with Return Value Check
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<string> col = new Collection<string>();
col.Add("One");
col.Add("Two");
col.Add("Two");
col.Add("Four");
col.Add("Five");
col.Add("Two");
col.Add("Six");
col.Add("Seven");
Console.WriteLine("Original collection:");
foreach(string item in col) {
Console.WriteLine(item);
}
// Remove first occurrence of "Two"
bool result1 = col.Remove("Two");
Console.WriteLine("\nRemoving 'Two': " + result1);
// Try to remove non-existent item
bool result2 = col.Remove("Ten");
Console.WriteLine("Removing 'Ten': " + result2);
Console.WriteLine("\nFinal collection:");
foreach(string item in col) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Original collection: One Two Two Four Five Two Six Seven Removing 'Two': True Removing 'Ten': False Final collection: One Two Four Five Two Six Seven
Common Use Cases
- Duplicate Management: Remove only the first duplicate while preserving others
- Selective Removal: Remove specific items without affecting similar entries
- Data Cleanup: Remove unwanted entries while maintaining collection order
Conclusion
The Collection<T>.Remove() method provides an efficient way to remove the first occurrence of a specified object from a collection. It returns a boolean value indicating success and preserves the order of remaining elements, making it ideal for selective data removal operations.
