List<T>.GetRange(Int32, Int32) List<T>.GetRange(Int32, Int32) List<T>.GetRange(Int32, Int32) List<T>.GetRange(Int32, Int32) Method

定義

為來源 List<T> 中的項目範圍建立淺層複本。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)

參數

index
Int32 Int32 Int32 Int32

範圍起始處之以零為起始的 List<T> 索引。The zero-based List<T> index at which the range starts.

count
Int32 Int32 Int32 Int32

範圍中的項目數。The number of elements in the range.

傳回

來源 List<T> 中項目範圍的淺層複本。A shallow copy of a range of elements in the source List<T>.

例外狀況

index 小於 0。index is less than 0.

-或--or- count 小於 0。count is less than 0.

indexcount 不代表 List<T> 中項目的有效範圍。index and count do not denote a valid range of elements in the List<T>.

範例

下列範例示範GetRange以及的其他方法List<T>處理範圍的類別。The following example demonstrates the GetRange method and other methods of the List<T> class that act on ranges. 在此範例中,結尾GetRange方法用來從清單中,開頭的索引位置 2 取得三個項目。At the end of the example, the GetRange method is used to get three items from the list, beginning with index location 2. ToArray方法呼叫所產生的List<T>,建立三個元素的陣列。The ToArray method is called on the resulting List<T>, creating an array of three elements. 陣列的元素會顯示。The elements of the array are displayed.

using namespace System;
using namespace System::Collections::Generic;

void main()
{
    array<String^>^ input = { "Brachiosaurus", 
                              "Amargasaurus", 
                              "Mamenchisaurus" };

    List<String^>^ dinosaurs = 
        gcnew List<String^>((IEnumerable<String^>^) input);

    Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nAddRange(dinosaurs)");
    dinosaurs->AddRange(dinosaurs);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nRemoveRange(2, 2)");
    dinosaurs->RemoveRange(2, 2);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    input = gcnew array<String^> { "Tyrannosaurus", 
                                   "Deinonychus", 
                                   "Velociraptor"};

    Console::WriteLine("\nInsertRange(3, (IEnumerable<String^>^) input)");
    dinosaurs->InsertRange(3, (IEnumerable<String^>^) input);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\noutput = dinosaurs->GetRange(2, 3)->ToArray()");
    array<String^>^ output = dinosaurs->GetRange(2, 3)->ToArray();
        
    Console::WriteLine();
    for each(String^ dinosaur in output )
    {
        Console::WriteLine(dinosaur);
    }
}

/* This code example produces the following output:

Capacity: 3

Brachiosaurus
Amargasaurus
Mamenchisaurus

AddRange(dinosaurs)

Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus

RemoveRange(2, 2)

Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus

InsertRange(3, (IEnumerable<String^>^) input)

Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus

output = dinosaurs->GetRange(2, 3)->ToArray()

Amargasaurus
Tyrannosaurus
Deinonychus
 */
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        string[] input = { "Brachiosaurus", 
                           "Amargasaurus", 
                           "Mamenchisaurus" };

        List<string> dinosaurs = new List<string>(input);

        Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nAddRange(dinosaurs)");
        dinosaurs.AddRange(dinosaurs);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nRemoveRange(2, 2)");
        dinosaurs.RemoveRange(2, 2);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        input = new string[] { "Tyrannosaurus", 
                               "Deinonychus", 
                               "Velociraptor"};

        Console.WriteLine("\nInsertRange(3, input)");
        dinosaurs.InsertRange(3, input);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\noutput = dinosaurs.GetRange(2, 3).ToArray()");
        string[] output = dinosaurs.GetRange(2, 3).ToArray();
        
        Console.WriteLine();
        foreach( string dinosaur in output )
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Capacity: 3

Brachiosaurus
Amargasaurus
Mamenchisaurus

AddRange(dinosaurs)

Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus

RemoveRange(2, 2)

Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus

InsertRange(3, input)

Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus

output = dinosaurs.GetRange(2, 3).ToArray()

Amargasaurus
Tyrannosaurus
Deinonychus
 */
Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim input() As String = { "Brachiosaurus", _
                                  "Amargasaurus", _
                                  "Mamenchisaurus" }

        Dim dinosaurs As New List(Of String)(input)

        Console.WriteLine(vbLf & "Capacity: {0}", dinosaurs.Capacity)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "AddRange(dinosaurs)")
        dinosaurs.AddRange(dinosaurs)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "RemoveRange(2, 2)")
        dinosaurs.RemoveRange(2, 2)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        input = New String() { "Tyrannosaurus", _
                               "Deinonychus", _
                               "Velociraptor" }

        Console.WriteLine(vbLf & "InsertRange(3, input)")
        dinosaurs.InsertRange(3, input)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "output = dinosaurs.GetRange(2, 3).ToArray")
        Dim output() As String = dinosaurs.GetRange(2, 3).ToArray()
        
        Console.WriteLine()
        For Each dinosaur As String In output
            Console.WriteLine(dinosaur)
        Next

    End Sub
End Class

' This code example produces the following output:
'
'Capacity: 3
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'AddRange(dinosaurs)
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'RemoveRange(2, 2)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Mamenchisaurus
'
'InsertRange(3, input)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Tyrannosaurus
'Deinonychus
'Velociraptor
'Mamenchisaurus
'
'output = dinosaurs.GetRange(2, 3).ToArray
'
'Amargasaurus
'Tyrannosaurus
'Deinonychus

備註

淺層複本的參考類型的集合或子集該集合中,只包含參考至集合的項目。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.

這個方法是 O (n) 運算,其中ncountThis method is an O(n) operation, where n is count.

適用於

另請參閱