Ler em inglês

Compartilhar via


Enumerable.Append<TSource>(IEnumerable<TSource>, TSource) Método

Definição

Acrescenta um valor ao final da sequência.

C#
public static System.Collections.Generic.IEnumerable<TSource> Append<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource element);

Parâmetros de tipo

TSource

O tipo dos elementos de source.

Parâmetros

source
IEnumerable<TSource>

Uma sequência de valores.

element
TSource

O valor a ser acrescentado a source.

Retornos

IEnumerable<TSource>

Uma nova sequência que termina com element.

Exceções

source é null.

Exemplos

O exemplo de código a seguir demonstra como usar Append para acrescentar um valor ao final da sequência.

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

// Trying to append any value of the same type
numbers.Append(5);

// 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.Append(5)));

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

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

// This code produces the following output:
//
// 1, 2, 3, 4
// 1, 2, 3, 4, 5
// 1, 2, 3, 4, 5

Comentários

Observação

Esse método não modifica os elementos da coleção. Em vez disso, ele cria uma cópia da coleção com o novo elemento .

Aplica-se a

Produto Versões
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.6, 2.0, 2.1