
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# ArrayList - RemoveRange() Method
The C# ArrayList RemoveRange() method is used to remove or discard a series of elements from an ArrayList, starting at the specified index and removing up to the specified number of elements.
Syntax
Following is the syntax of the C# ArrayList RemoveRange() method −
public virtual void RemoveRange (int index, int count);
Parameters
This method accepts a single parameter −
- index: It is a zero-based starting index of the range of elements to remove.
- count: It is the number of element to remove.
Return value
This method does not return any value.
Example 1: Remove Integer in Range from ArrayList.
Following is the basic example of the RemoveRange() method to remove the elements in range from ArrayList −
using System; using System.Collections; class Program { static void Main() { ArrayList arrayList = new ArrayList { 1, 2, 2, 3, 4 }; Console.Write("Initial ArrayList: "); foreach (var item in arrayList) { Console.Write(item + " "); } // remove total 2 elements // starting from index 2 arrayList.RemoveRange(2, 2); Console.Write( "\nUpdated ArrayList: "); foreach (var item in arrayList) { Console.Write(item + " "); } } }
Output
Following is the output −
Initial ArrayList: 1 2 2 3 4 Updated ArrayList: 1 2 4
Example 2: Remove String in Range from ArrayList
Let's see another example where we remove a string in the range from the starting index to the number of elements from the ArrayList using the RemoveRange() method −
using System; using System.Collections; class Program { static void Main() { ArrayList arrayList = new ArrayList { "Hi", "tutorialspoint", "India", "tutorix", "Hyderabad" }; Console.Write("Initial arrayList: "); foreach (var item in arrayList) { Console.Write(item + " "); } // remove total 3 elements // starting from index 2 arrayList.RemoveRange(2, 3); Console.Write( "\nUpdated ArrayList: "); foreach (var item in arrayList) { Console.Write(item + " "); } } }
Output
Following is the output −
Initial arrayList: Hi tutorialspoint India tutorix Updated ArrayList: Hi tutorialspoint India
Example 3: What if The Range Exceed the ArrayList Bound?
The below example throws an exception 'ArgumentException' if the range exceeds the bound of the ArrayList −
using System; using System.Collections; class Program { static void Main() { ArrayList arrayList = new ArrayList {1, 2, 3, 4, 5}; // remove total 5 elements // starting from index 2 arrayList.RemoveRange(2, 5); Console.Write( "\nUpdated ArrayList: "); foreach (var item in arrayList) { Console.Write(item + " "); } } }
Output
Following is the output −
Unhandled Exception: System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
Example 4: Managing a Task List
Here, we create an ArrayList that stores some completed and incomplete tasks. we use the RemoveRange() method to discard the completed task from ArrayList −
using System; using System.Collections; class Task { public string Name { get; set; } public bool IsCompleted { get; set; } public Task(string name, bool isCompleted) { Name = name; IsCompleted = isCompleted; } public override string ToString() { return $"Task: {Name}, Completed: {IsCompleted}"; } } class Program { static void Main() { // Create an ArrayList to store tasks ArrayList tasks = new ArrayList() { new Task("Design UI", true), new Task("Set up database", true), new Task("Write API endpoints", true), new Task("Test API endpoints", true), new Task("Develop frontend", false), new Task("Integrate API with frontend", false), new Task("Final testing", false) }; Console.WriteLine("Original Task List:"); foreach (Task task in tasks) Console.WriteLine(task); // Remove a range of completed tasks // from index 0 to index 3 Console.WriteLine("\nRemoving completed tasks (index 0 to 3)..."); tasks.RemoveRange(0, 4); Console.WriteLine("\nUpdated Task List:"); foreach (Task task in tasks) Console.WriteLine(task); } }
Output
Following is the output −
Original Task List: Task: Design UI, Completed: True Task: Set up database, Completed: True Task: Write API endpoints, Completed: True Task: Test API endpoints, Completed: True Task: Develop frontend, Completed: False Task: Integrate API with frontend, Completed: False Task: Final testing, Completed: False Removing completed tasks (index 0 to 3)... Updated Task List: Task: Develop frontend, Completed: False Task: Integrate API with frontend, Completed: False Task: Final testing, Completed: False