List<T>.InsertRange(Int32, IEnumerable<T>) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Insère les éléments d’une collection dans l’index List<T> spécifié.
public:
void InsertRange(int index, System::Collections::Generic::IEnumerable<T> ^ collection);
public void InsertRange(int index, System.Collections.Generic.IEnumerable<T> collection);
member this.InsertRange : int * seq<'T> -> unit
Public Sub InsertRange (index As Integer, collection As IEnumerable(Of T))
Paramètres
- index
- Int32
Index de base zéro auquel les nouveaux éléments doivent être insérés.
- collection
- IEnumerable<T>
Collection dont les éléments doivent être insérés dans le List<T>. La collection elle-même ne peut pas être null, mais elle peut contenir des éléments qui sont null, si le type T est un type référence.
Exceptions
collection a la valeur null.
Exemples
L’exemple suivant illustre InsertRange la méthode et d’autres méthodes de la List<T> classe qui agissent sur des plages. Une fois que la liste a été créée et remplie avec les noms de plusieurs dinosaures mangeurs de plantes pacifiques, la InsertRange méthode est utilisée pour insérer un tableau de trois dinosaures féroces mangeant de la viande dans la liste, commençant à l’emplacement d’index 3.
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
Remarques
List<T> accepte null comme valeur valide pour les types de référence et autorise les éléments en double.
Si le nouveau Count (actuel Count plus la taille de la collection) est supérieur Capacityà , la capacité de l’élément List<T> est augmentée en réaffectant automatiquement le tableau interne pour prendre en charge les nouveaux éléments et les éléments existants sont copiés dans le nouveau tableau avant l’ajout des nouveaux éléments.
S’il index est égal à Count, les éléments sont ajoutés à la fin de List<T>.
L’ordre des éléments de la collection est conservé dans le List<T>.
Cette méthode est une opération O(n * m), où n est le nombre d’éléments à ajouter et m est Count.