C# Enumerable Union(IEnumerable, IEnumerable)

Description

Produces the set union of two sequences by using the default equality comparer.

Syntax


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

Parameters

  • TSource - The type of the elements of the input sequences.
  • first - An IEnumerable whose distinct elements form the first set for the union.
  • second - An IEnumerable whose distinct elements form the second set for the union.

Example

The following code example demonstrates how to use Union to obtain the union of two sequences of integers.


/*from www  .ja v a 2s .  co m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

    int[] ints1 = { 5, 3, 9};
    int[] ints2 = { 8, 3, 6};

    IEnumerable<int> union = ints1.Union(ints2);

    foreach (int num in union)
    {
        Console.Write("{0} ", num);
    }

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable