List<T>.GetRange(Int32, Int32) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a shallow copy of a range of elements in the source List<T>.
public:
System::Collections::Generic::List<T> ^ GetRange(int index, int count);
public System.Collections.Generic.List<T> GetRange(int index, int count);
member this.GetRange : int * int -> System.Collections.Generic.List<'T>
Public Function GetRange (index As Integer, count As Integer) As List(Of T)
Parameters
- count
- Int32
The number of elements in the range.
Returns
A shallow copy of a range of elements in the source List<T>.
Exceptions
index and count do not denote a valid range of elements in the List<T>.
Examples
The following example demonstrates the GetRange method and other methods of the List<T> class that act on ranges. At the end of the example, the GetRange method is used to get three items from the list, beginning with index location 2. The ToArray method is called on the resulting List<T>, creating an array of three elements. The elements of the array are displayed.
using System;
using System.Collections.Generic;
string[] input = { "Apple",
"Banana",
"Orange" };
List<string> fruits = new List<string>(input);
Console.WriteLine("\nCapacity: {0}", fruits.Capacity);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\nAddRange(fruits)");
fruits.AddRange(fruits);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\nRemoveRange(2, 2)");
fruits.RemoveRange(2, 2);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
input = new string[] { "Mango",
"Pineapple",
"Watermelon" };
Console.WriteLine("\nInsertRange(3, input)");
fruits.InsertRange(3, input);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\noutput = fruits.GetRange(2, 3).ToArray()");
string[] output = fruits.GetRange(2, 3).ToArray();
Console.WriteLine();
foreach (string fruit in output)
{
Console.WriteLine(fruit);
}
/*
This code example produces the following output:
Capacity: 3
Apple
Banana
Orange
AddRange(fruits)
Apple
Banana
Orange
Apple
Banana
Orange
RemoveRange(2, 2)
Apple
Banana
Banana
Orange
InsertRange(3, input)
Apple
Banana
Banana
Mango
Pineapple
Watermelon
Orange
output = fruits.GetRange(2, 3).ToArray()
Banana
Mango
Pineapple
*/
Imports System.Collections.Generic
Partial Public Class Program
Public Shared Sub ShowFruits()
Dim input() As String = { "Apple", _
"Banana", _
"Orange" }
Dim fruits As New List(Of String)(input)
Console.WriteLine(vbLf & "Capacity: {0}", fruits.Capacity)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "AddRange(fruits)")
fruits.AddRange(fruits)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "RemoveRange(2, 2)")
fruits.RemoveRange(2, 2)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
input = New String() { "Mango", _
"Pineapple", _
"Watermelon" }
Console.WriteLine(vbLf & "InsertRange(3, input)")
fruits.InsertRange(3, input)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "output = fruits.GetRange(2, 3).ToArray")
Dim output() As String = fruits.GetRange(2, 3).ToArray()
Console.WriteLine()
For Each fruit As String In output
Console.WriteLine(fruit)
Next
End Sub
End Class
' This code example produces the following output:
'
' Capacity: 3
'
' Apple
' Banana
' Orange
'
' AddRange(fruits)
'
' Apple
' Banana
' Orange
' Apple
' Banana
' Orange
'
' RemoveRange(2, 2)
'
' Apple
' Banana
' Banana
' Orange
'
' InsertRange(3, input)
'
' Apple
' Banana
' Banana
' Mango
' Pineapple
' Watermelon
' Orange
'
' output = fruits.GetRange(2, 3).ToArray
'
' Banana
' Mango
' Pineapple
Remarks
A shallow copy of a collection of reference types, or a subset of that collection, contains only the references to the elements of the collection. The objects themselves are not copied. The references in the new list point to the same objects as the references in the original list.
A shallow copy of a collection of value types, or a subset of that collection, contains the elements of the collection. However, if the elements of the collection contain references to other objects, those objects are not copied. The references in the elements of the new collection point to the same objects as the references in the elements of the original collection.
In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.
This method is an O(n) operation, where n is count.