Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy the elements of collection over a range of elements in ArrayList in C#
To copy the elements of the collection over a range of elements in ArrayList, the code is as follows −
Example
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]);
}
}
}
Output
This will produce the following output −
ArrayList elements... A B C D After copying... Demo Text C D
Example
Let us now see another example −
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]);
}
}
}
Output
This will produce the following output −
ArrayList elements... One Two Three Four Five Six Seven Eight After copying... One Two Demo Text Five Six Seven Eight
Advertisements