C# Enumerable Concat

Description

Concatenates two sequences.

Syntax


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

Parameters

  • TSource - The type of the elements of the input sequences.
  • first - The first sequence to concatenate.
  • second - The sequence to concatenate to the first sequence.

Example

The following code example demonstrates how to use Concat to concatenate two sequences.


/*  w ww  . j  a  v  a  2 s.  c  o m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
      Employee[] g1 = Group1();
      Employee[] g2 = Group2();

      IEnumerable<string> query =
          g1.Select(cat => cat.Name).Concat(g2.Select(dog => dog.Name));

      foreach (string name in query)
      {
          Console.WriteLine(name);
      }
  }
  static Employee[] Group1()
  {
      Employee[] g1 = { new Employee { Name="A", Age=38 },
                     new Employee { Name="B", Age=34 },
                     new Employee { Name="C", Age=31 } };
      return g1;
  }

  static Employee[] Group2()
  {
      Employee[] g2 = { new Employee { Name="D", Age=33 },
                     new Employee { Name="E", Age=34 },
                     new Employee { Name="F", Age=39 } };
      return g2;
  }
}
    
class Employee{
   public string Name { get; set; }
   public int Age { get; set; }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable