Enumerable.Prepend<TSource>(IEnumerable<TSource>, TSource) Méthode

Définition

Ajoute une valeur au début de la séquence.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ Prepend(System::Collections::Generic::IEnumerable<TSource> ^ source, TSource element);
public static System.Collections.Generic.IEnumerable<TSource> Prepend<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource element);
static member Prepend : seq<'Source> * 'Source -> seq<'Source>
<Extension()>
Public Function Prepend(Of TSource) (source As IEnumerable(Of TSource), element As TSource) As IEnumerable(Of TSource)

Paramètres de type

TSource

Le type des éléments de source.

Paramètres

source
IEnumerable<TSource>

Séquence de valeurs.

element
TSource

Valeur à ajouter à source.

Retours

IEnumerable<TSource>

Nouvelle séquence qui commence par element.

Exceptions

source a la valeur null.

Exemples

L’exemple de code suivant montre comment utiliser Prepend pour ajouter une valeur au début de la séquence.

// Creating a list of numbers
List<int> numbers = new List<int> { 1, 2, 3, 4 };

// Trying to prepend any value of the same type
numbers.Prepend(0);

// It doesn't work because the original list has not been changed
Console.WriteLine(string.Join(", ", numbers));

// It works now because we are using a changed copy of the original list
Console.WriteLine(string.Join(", ", numbers.Prepend(0)));

// If you prefer, you can create a new list explicitly
List<int> newNumbers = numbers.Prepend(0).ToList();

// And then write to the console output
Console.WriteLine(string.Join(", ", newNumbers));

// This code produces the following output:
//
// 1, 2, 3, 4
// 0, 1, 2, 3, 4
// 0, 1, 2, 3, 4
' Creating a list of numbers
Dim numbers As New List(Of Integer)(New Integer() {1, 2, 3, 4})

' Trying to prepend any value of the same type
numbers.Prepend(0)

' It doesn't work because the original list has not been changed
Console.WriteLine(String.Join(", ", numbers))

' It works now because we are using a changed copy of the original list
Console.WriteLine(String.Join(", ", numbers.Prepend(0)))

' If you prefer, you can create a new list explicitly
Dim newNumbers As List(Of Integer) = numbers.Prepend(0).ToList

' And then write to the console output
Console.WriteLine(String.Join(", ", newNumbers))

' This code produces the following output:
'
' 1, 2, 3, 4
' 0, 1, 2, 3, 4
' 0, 1, 2, 3, 4

Remarques

Remarque

Cette méthode ne modifie pas les éléments de la collection. Au lieu de cela, il crée une copie de la collection avec le nouvel élément .

S’applique à