C# ArrayList - GetRange() Method



The C# ArrayList GetRange() method is used to create a subset of an ArrayList. This method creates and returns a new ArrayList containing a subset of elements from the source ArrayList.

Syntax

Following is the syntax of the C# ArrayList GetRange() method −

public virtual System.Collections.ArrayList GetRange(int index, int count);

Parameters

This method accepts the following parameter −

  • index: It represents a ArrayList index at which the range starts.
  • count: It 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.

Example 1: Create a Subset of Elements

Following is the basic example of the GetRange() method to create a subset of the element from index 2, for a total 3 element −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList sourceList = new ArrayList() { 1, 2, 3, 4, 5, 6, 7 };

      Console.WriteLine("Source ArrayList:");
      foreach (var item in sourceList)
      {
         Console.Write(item + " ");
      }

      // Create a subset of elements 
	  // from index 2, take 3 elements
      ArrayList subset = sourceList.GetRange(2, 3);

      Console.WriteLine("\n\nSubset ArrayList:");
      foreach (var item in subset)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Source ArrayList:
1 2 3 4 5 6 7 

Subset ArrayList:
3 4 5 

Example 2: Create a Subset of Mixed-Type Elements

If the ArrayList contains mixed data types, GetRange() will return the subset as is, preserving the data types of the elements −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList mixedList = new ArrayList() { "Hello", 42, 'A', 3.14, "World", true };

      Console.WriteLine("Source ArrayList:");
      foreach (var item in mixedList)
      {
         Console.Write(item + " ");
      }

      // Create a subset of elements
      ArrayList subset = mixedList.GetRange(1, 4);

      Console.WriteLine("\nSubset ArrayList:");
      foreach (var item in subset)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Source ArrayList:
Hello 42 A 3.14 World True 
Subset ArrayList:
42 A 3.14 World

Example 3: Trying to Access Elements Outside Range

In this example, we use the GetRange() method to attempt to retrieve a subset from the ArrayList. If the range exceeds the boundaries of the ArrayList, this method will throw an 'ArgumentException' −

using System;
using System.Collections;

class Example
{
   static void Main()
   {
      ArrayList list = new ArrayList() { 1, 2, 3, 4, 5 };

      try
      {
          
         ArrayList subset = list.GetRange(3, 5); // Index out of bounds
      }
      catch (ArgumentException ex)
      {
         Console.WriteLine("Error: " + ex.Message);
      }
   }
}

Output

Following is the output −

ERROR!
Error: 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.
csharp_arraylist.htm
Advertisements