.NET API Documentation

System.Linq.Enumerable.Concat Method

Appends the elements of the specified sequence to the end of the specified sequence.

public static IEnumerable<TSource> Concat<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second );

Parameters

  • first: IEnumerable<TSource>
    The first sequence to append to.
  • second: IEnumerable<TSource>
    The sequence to append to the end of the first sequence.

Returns

IEnumerable<TSource>
An IEnumerable<TSource> that contains the elements of the first sequence followed by the elements of the second sequence.

Remarks

The Concat method concatenates two sequences. It returns an enumerator that collects the elements of the first sequence, followed by the elements of the second sequence. If either first or second is null, an ArgumentNullException is thrown.

Examples

// Sample usage of Concat.
// The following code example demonstrates the use of the Concat method.

// Define two integer arrays.
int[] numbers1 = { 1, 2, 3 };
int[] numbers2 = { 4, 5, 6 };

// Concatenate the two arrays using the Concat method.
IEnumerable<int> concatenatedNumbers = numbers1.Concat(numbers2);

// Iterate through the concatenated sequence and print the elements.
Console.WriteLine("Concatenated numbers:");
foreach (int number in concatenatedNumbers)
{
    Console.Write(number + " ");
}
// Expected output:
// Concatenated numbers:
// 1 2 3 4 5 6
C#