System.Linq.Enumerable.Append Method

Defined in System.Linq

Summary

Appends an element to the end of a sequence.

Syntax

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

Parameters

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

Returns

Type Description
IEnumerable<TSource> An IEnumerable<T> that ends with the specified element.

Examples

using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
    public static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3 };

        IEnumerable<int> appendedNumbers = numbers.Append(4);

        // The appendedNumbers sequence will be { 1, 2, 3, 4 }
        foreach (var num in appendedNumbers)
        {
            Console.Write($"{num} ");
        }
        // Output: 1 2 3 4
    }
}

Remarks

The Append method returns an enumerable that contains all elements in the source sequence followed by the specified element.

This method does not modify the original sequence; it creates a new enumerable with the appended element.

If the source is null, a ArgumentNullException is thrown.