Append Method

In namespace: System.Linq

Appends an element to the end of a sequence.

Syntax

public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element)

Parameters

Name Description
source An IEnumerable<TSource> to append the element to.
element The element to append to the sequence.

Type Parameters

TSource

The type of the elements of source.

Remarks

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.

Example

// 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

See Also