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
Get the range of elements in a C# list
The GetRange() method in C# is used to extract a subset of elements from a List<T>. This method returns a new list containing the specified range of elements from the original list, without modifying the original list.
Syntax
Following is the syntax for the GetRange() method −
public List<T> GetRange(int index, int count)
Parameters
index − The zero-based starting index of the range to extract.
count − The number of elements to extract from the starting index.
Return Value
Returns a new List<T> containing the specified range of elements. The original list remains unchanged.
Using GetRange() Method
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> arr1 = new List<int>();
arr1.Add(10);
arr1.Add(20);
arr1.Add(30);
arr1.Add(40);
arr1.Add(50);
Console.WriteLine("Initial List ...");
foreach (int i in arr1) {
Console.WriteLine(i);
}
Console.WriteLine("Getting elements between a range...");
List<int> myList = arr1.GetRange(1, 3);
foreach (int res in myList) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Initial List ... 10 20 30 40 50 Getting elements between a range... 20 30 40
GetRange() with Different Data Types
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> colors = new List<string>
{ "Red", "Blue", "Green", "Yellow", "Orange", "Purple" };
Console.WriteLine("Original list:");
foreach (string color in colors) {
Console.WriteLine(color);
}
List<string> selectedColors = colors.GetRange(2, 3);
Console.WriteLine("\nSelected range (index 2, count 3):");
foreach (string color in selectedColors) {
Console.WriteLine(color);
}
}
}
The output of the above code is −
Original list: Red Blue Green Yellow Orange Purple Selected range (index 2, count 3): Green Yellow Orange
Error Handling with GetRange()
The GetRange() method throws an ArgumentException if the specified range is invalid −
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
try {
List<int> validRange = numbers.GetRange(1, 3);
Console.WriteLine("Valid range extracted successfully");
List<int> invalidRange = numbers.GetRange(3, 5);
}
catch (ArgumentException ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Valid range extracted successfully 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.
Conclusion
The GetRange() method provides an efficient way to extract a subset of elements from a C# list. It creates a new list containing the specified range without modifying the original list, making it useful for data manipulation and filtering operations.
