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
Copy the elements of collection over a range of elements in ArrayList in C#
The SetRange() method in C# ArrayList allows you to copy elements from a collection over a specific range of elements in the ArrayList. This method replaces existing elements starting from the specified index with elements from the source collection.
Syntax
Following is the syntax for the SetRange() method −
public virtual void SetRange(int index, ICollection c)
Parameters
-
index − The zero-based index in the ArrayList at which to start copying elements.
-
c − The ICollection whose elements are to be copied to the ArrayList. The collection itself cannot be null, but it can contain elements that are null.
Using SetRange() from Beginning
This example demonstrates copying elements from index 0 −
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList arrList = new ArrayList();
arrList.Add("A");
arrList.Add("B");
arrList.Add("C");
arrList.Add("D");
Console.WriteLine("ArrayList elements...");
for (int i = 0; i < arrList.Count; i++) {
Console.WriteLine("" + arrList[i]);
}
string[] str = { "Demo", "Text" };
arrList.SetRange(0, str);
Console.WriteLine("After copying...");
for (int i = 0; i < arrList.Count; i++) {
Console.WriteLine("" + arrList[i]);
}
}
}
The output of the above code is −
ArrayList elements... A B C D After copying... Demo Text C D
Using SetRange() from Specific Index
This example shows copying elements starting from index 2 −
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList arrList = new ArrayList();
arrList.Add("One");
arrList.Add("Two");
arrList.Add("Three");
arrList.Add("Four");
arrList.Add("Five");
arrList.Add("Six");
arrList.Add("Seven");
arrList.Add("Eight");
Console.WriteLine("ArrayList elements...");
for (int i = 0; i < arrList.Count; i++) {
Console.WriteLine("" + arrList[i]);
}
string[] str = { "Demo", "Text" };
arrList.SetRange(2, str);
Console.WriteLine("After copying...");
for (int i = 0; i < arrList.Count; i++) {
Console.WriteLine("" + arrList[i]);
}
}
}
The output of the above code is −
ArrayList elements... One Two Three Four Five Six Seven Eight After copying... One Two Demo Text Five Six Seven Eight
Using SetRange() with Different Collection Types
The SetRange() method accepts any ICollection, including arrays, Lists, and other ArrayLists −
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main(){
ArrayList arrList = new ArrayList();
arrList.Add(1);
arrList.Add(2);
arrList.Add(3);
arrList.Add(4);
List<int> numbers = new List<int> { 10, 20, 30 };
Console.WriteLine("Original ArrayList:");
foreach (var item in arrList) {
Console.WriteLine(item);
}
arrList.SetRange(1, numbers);
Console.WriteLine("After SetRange with List:");
foreach (var item in arrList) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Original ArrayList: 1 2 3 4 After SetRange with List: 1 10 20 30
Conclusion
The SetRange() method provides an efficient way to replace a range of elements in an ArrayList with elements from any ICollection. It modifies the existing ArrayList in-place, starting from the specified index and replacing as many elements as are in the source collection.
