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
Remove a range of elements from the List in C#
The RemoveRange() method in C# allows you to remove multiple elements from a List<T> at once by specifying a starting index and the number of elements to remove. This is more efficient than removing elements one by one.
Syntax
Following is the syntax for the RemoveRange() method −
public void RemoveRange(int index, int count)
Parameters
index − The zero-based starting index of the range of elements to remove.
count − The number of elements to remove starting from the specified index.
Using RemoveRange() to Remove Multiple Elements
Example
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);
// Remove 3 elements starting from index 1 (US, UK, Canada)
countries.RemoveRange(1, 3);
Console.WriteLine("Count after removal: " + countries.Count);
Console.WriteLine("\nUpdated 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 Count after removal: 3 Updated List: India Poland Netherlands
Multiple RemoveRange() Operations
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> list = new List<string>();
list.Add("India");
list.Add("US");
list.Add("UK");
list.Add("Canada");
list.Add("Poland");
list.Add("Netherlands");
list.Add("Bhutan");
list.Add("Singapore");
Console.WriteLine("Original List:");
foreach (string item in list) {
Console.WriteLine(item);
}
Console.WriteLine("\nFirst RemoveRange(1, 3):");
list.RemoveRange(1, 3);
Console.WriteLine("Count: " + list.Count);
foreach (string item in list) {
Console.WriteLine(item);
}
Console.WriteLine("\nSecond RemoveRange(2, 2):");
list.RemoveRange(2, 2);
Console.WriteLine("Count: " + list.Count);
foreach (string item in list) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Original List: India US UK Canada Poland Netherlands Bhutan Singapore First RemoveRange(1, 3): Count: 5 India Poland Netherlands Bhutan Singapore Second RemoveRange(2, 2): Count: 3 India Poland Singapore
Key Points
The
RemoveRange()method modifies the original list by removing the specified elements.Elements after the removed range are shifted to fill the gap, maintaining the list's contiguous structure.
If the starting index or count parameters are invalid, an
ArgumentOutOfRangeExceptionis thrown.This method is more efficient than calling
Remove()multiple times for consecutive elements.
Conclusion
The RemoveRange() method provides an efficient way to remove multiple consecutive elements from a List<T> in a single operation. It requires specifying the starting index and the number of elements to remove, making it ideal for bulk deletion operations.
