Enumerable.Concat<TSource> Method

Definition

Concatenates two sequences.

C#
public static System.Collections.Generic.IEnumerable<TSource> Concat<TSource>(this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);

Type Parameters

TSource

The type of the elements of the input sequences.

Parameters

first
IEnumerable<TSource>

The first sequence to concatenate.

second
IEnumerable<TSource>

The sequence to concatenate to the first sequence.

Returns

IEnumerable<TSource>

An IEnumerable<T> that contains the concatenated elements of the two input sequences.

Exceptions

first or second is null.

Examples

The following code example demonstrates how to use Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) to concatenate two sequences.

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

static Pet[] GetCats()
{
    Pet[] cats = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };
    return cats;
}

static Pet[] GetDogs()
{
    Pet[] dogs = { new Pet { Name="Bounder", Age=3 },
                   new Pet { Name="Snoopy", Age=14 },
                   new Pet { Name="Fido", Age=9 } };
    return dogs;
}

public static void ConcatEx1()
{
    Pet[] cats = GetCats();
    Pet[] dogs = GetDogs();

    IEnumerable<string> query =
        cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));

    foreach (string name in query)
    {
        Console.WriteLine(name);
    }
}

// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido

An alternative way of concatenating two sequences is to construct a collection, for example an array, of sequences and then apply the SelectMany method, passing it the identity selector function. The following example demonstrates this use of SelectMany.

C#
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();

IEnumerable<string> query =
    new[] { cats.Select(cat => cat.Name), dogs.Select(dog => dog.Name) }
    .SelectMany(name => name);

foreach (string name in query)
{
    Console.WriteLine(name);
}

// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

The Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method differs from the Union method because the Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method returns all the original elements in the input sequences. The Union method returns only unique elements.

Applies to

Product Versions
.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 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0