In namespace: System.Linq
Appends an element to the end of a sequence.
public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element)
Name | Description |
---|---|
source |
An IEnumerable<TSource> to append the element to. |
element |
The element to append to the sequence. |
TSource
The type of the elements of source
.
The Append
method returns a new sequence that contains all elements in the input sequence, followed by the specified element.
This method does not modify the original sequence.
If the input sequence implements IList<T>
, the Append
operation can be more efficient.
// Sample array
int[] numbers = { 1, 2, 3, 4 };
// Append an element
IEnumerable<int> appendedSequence = numbers.Append(5);
// Print the new sequence
foreach (int number in appendedSequence)
{
Console.WriteLine(number);
}
// Output:
// 1
// 2
// 3
// 4
// 5