ArrayList.GetRange(Int32, Int32) Method is used to get an ArrayList which will represent a subset of the elements in the source ArrayList.
Syntax:
public virtual System.Collections.ArrayList GetRange (int index, int count);
Parameters:
index: It is of Int32 type and represents the zero-based ArrayList index at which the range starts.
count: It is of Int32 type and represents the number of elements in the range.
Return Value: This method returns an ArrayList which represents a subset of the elements in the source ArrayList.
Exceptions:
- ArgumentOutOfRangeException: If the value of the index is less than zero, or if the value of count is less than zero.
- ArgumentException: If the value of an index and count does not denote a valid range of elements in the ArrayList.
Below programs illustrate the above-discussed method:
Example 1:
// C# program to illustrate the // concept of GetRange() Method using System; using System.Collections; class GFG { // Main method public static void Main() { // Creates and initializes // a new ArrayList. ArrayList myarraylist = new ArrayList(); myarraylist.Add("Welcome"); myarraylist.Add("to"); myarraylist.Add("Geeks"); myarraylist.Add("for"); myarraylist.Add("Geeks"); myarraylist.Add("portal"); // Creates and initializes queue Queue mynewList = new Queue(); mynewList.Enqueue("This"); mynewList.Enqueue("is"); mynewList.Enqueue("C#"); mynewList.Enqueue("tutorial"); // Displays the values of six // elements starting at index 0. ArrayList newarraylist = myarraylist.GetRange(0, 6); Console.WriteLine("Elements are:"); Displaydata(newarraylist, '\n'); // Replaces the values of six elements // starting at index 1 with the values // in the queue. myarraylist.SetRange(2, mynewList); // Displays the values of six // elements starting at index 0. newarraylist = myarraylist.GetRange(0, 6); Console.WriteLine("\nNow elements are:"); Displaydata(newarraylist, '\n'); } public static void Displaydata(IEnumerable myvalueList, char mySeparator) { foreach(Object obj in myvalueList) Console.Write("{0}{1}", mySeparator, obj); Console.WriteLine(); } } |
Output:
Elements are: Welcome to Geeks for Geeks portal Now elements are: Welcome to This is C# tutorial
Example 2:
// C# program to illustrate the // concept of GetRange() Method using System; using System.Collections; class GFG { // Main method public static void Main() { // Creates and initializes a new ArrayList. ArrayList myarraylist = new ArrayList(); myarraylist.Add("Welcome"); myarraylist.Add("to"); myarraylist.Add("Geeks"); myarraylist.Add("for"); myarraylist.Add("Geeks"); myarraylist.Add("portal"); // Creates and initializes queue Queue mynewList = new Queue(); mynewList.Enqueue("This"); mynewList.Enqueue("is"); mynewList.Enqueue("C#"); mynewList.Enqueue("tutorial"); // Displays the values of six elements ArrayList newarraylist = myarraylist.GetRange(-1, 6); Console.WriteLine("Elements are:"); Displaydata(newarraylist, '\n'); // Replaces the values of six elements // starting at index 1 with the // values in the queue. myarraylist.SetRange(2, mynewList); // Displays the values of six // elements starting at index 0. newarraylist = myarraylist.GetRange(0, 6); Console.WriteLine("Now elements are:"); Displaydata(newarraylist, '\n'); } public static void Displaydata(IEnumerable myvalueList, char mySeparator) { foreach(Object obj in myvalueList) Console.Write("{0}{1}", mySeparator, obj); Console.WriteLine(); } } |
Runtime Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: index
Reference:
Recommended Posts:
- C# | Getting an enumerator for a range of elements in the ArrayList
- C# | Getting an enumerator for the entire ArrayList
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | Get the number of elements actually contained in the ArrayList
- C# | Copying the elements of ArrayList to a new array
- C# | Get or set the number of elements that the ArrayList can contain
- C# | Remove a range of elements from the ArrayList
- C# | Remove all elements from the ArrayList
- C# | Reverse the order of the elements in the entire ArrayList or in the specified range
- C# | ArrayList whose elements are copies of the specified value
- C# | Sort the elements in the ArrayList
- C# | Sets the capacity to the actual number of elements in the ArrayList
- C# | Adding elements to the end of the ArrayList
- C# | Adding the elements to the end of the ArrayList
- C# | Remove the first occurrence of a specific object from the ArrayList
- C# | Check whether an element is contained in the ArrayList
- C# | Check if the ArrayList is read-only
- C# | Insert an element into the ArrayList at the specified index
- C# | Add an object to the end of the ArrayList
- C# | Check if the ArrayList has a fixed size
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.

