C# | Copy the elements of collection over a range of elements in ArrayList
ArrayList.SetRange(Int32, ICollection) Method is used to copy the elements of a collection over a range of elements in the ArrayList.
Syntax:
public virtual void SetRange (int index, System.Collections.ICollection c);
Parameters:
index: It is a zero-based ArrayList index at which to start copying the elements of c. The type of this parameter is System.Int32.
c: It is an ICollection whose elements to copy to the ArrayList. The collection itself cannot be null, but it can contain elements that are null.
Exceptions:
- ArgumentNullException: If the value of c is null.
- NotSupportedException: If the ArrayList is read-only.
- ArgumentOutOfRangeException: If the index is less than zero or [index + number of elements in c] > Count.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# Program to illustrate the // SetRange() Method using System; using System.Collections; class GFG { // Main method public static void Main() { // Create and initialize ArrayList ArrayList mylist = new ArrayList(); mylist.Add("G"); mylist.Add("e"); mylist.Add("e"); mylist.Add("k"); mylist.Add("s"); mylist.Add("G"); mylist.Add("F"); mylist.Add("G"); // There are total 4 elements string[] str = { "This", "is", "C#", "Tutorial" }; // using SetRange() Method // starting from index 0 mylist.SetRange(0, str); Show("ArrayList is", mylist); } // show method to display the result static void Show(string arr, ArrayList mylist) { for (int j = 0; j < mylist.Count; j++) { Console.WriteLine(arr + "[" + j + "] = " + mylist[j]); } } } |
ArrayList is[0] = This ArrayList is[1] = is ArrayList is[2] = C# ArrayList is[3] = Tutorial ArrayList is[4] = s ArrayList is[5] = G ArrayList is[6] = F ArrayList is[7] = G
Example 2:
// C# Program to illustrate the // SetRange() Method using System; using System.Collections; class GFG { // Main method public static void Main() { // Create and initialize ArrayList ArrayList mylist1 = new ArrayList(); mylist1.Add("Hello "); mylist1.Add("Welcome "); mylist1.Add("to "); mylist1.Add("online "); mylist1.Add("portal "); mylist1.Add("of "); mylist1.Add("Geeks "); mylist1.Add("for "); mylist1.Add("Geeks "); // Create and initialize new ArrayList ArrayList mylist2 = new ArrayList(); mylist2.Add("This "); mylist2.Add("is "); mylist2.Add("C# "); mylist2.Add("tutorial"); mylist2.Add("."); // Displays the values of 6 // elements of mylist1 starting // at index 0. ArrayList result = mylist1.GetRange(0, 6); Console.WriteLine("String from index number 0 to 6:"); { foreach(Object obj in result) Console.Write("{0}", obj); Console.WriteLine(); } // Replace the value of 5 element // starting from index 1 // with the values in mylist2 mylist1.SetRange(1, mylist2); // Display the result result = mylist1.GetRange(0, 6); Console.WriteLine("After SetRange() Method:"); { foreach(Object obj in result) Console.Write("{0}", obj); Console.WriteLine(); } } } |
String from index number 0 to 6: Hello Welcome to online portal of After SetRange() Method: Hello This is C# tutorial.
Reference:
Recommended Posts:
- C# | Getting an enumerator for a range of elements in the ArrayList
- C# | Remove a range of elements from the ArrayList
- C# | Reverse the order of the elements in the entire ArrayList or in the specified range
- C# | Remove all elements from the Collection<T>
- C# | Get the number of elements contained in Collection<T>
- C# | Remove all elements in a collection from a HashSet
- C# | Adding the elements of the specified collection to the end of the List
- C# | Copying the Collection<T> elements to an array
- C# | Check if SortedSet and the specified collection contain the same elements
- C# | Check if HashSet and the specified collection contain the same elements
- C# | ArrayList whose elements are copies of the specified value
- C# | Adding elements to the end of the ArrayList
- C# | Sort the elements in the ArrayList
- C# | Adding the elements to the end of the ArrayList
- C# | Get or set the number of elements that the ArrayList can contain
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



