The Union operator returns a sequence of the set union of two source sequences.
public static IEnumerable<T> Union<T>( this IEnumerable<T> first, IEnumerable<T> second);
ArgumentNullException is thrown if any arguments are null.
The following code shows the difference between the Union operator and the Concat operator.
The code will create a first and second sequence from our codeNames array.
We will then display the count of the codeNames array and the first and second sequences, as well as the count of a concatenated and union sequence.
using System; using System.Linq; using System.Collections; using System.Collections.Generic; class Program// ww w .ja va 2s .c om { static void Main(string[] args) { string[] codeNames = { "Python", "Java", "Javascript", "Bash", "C++", "Oracle" }; IEnumerable<string> first = codeNames.Take(5); IEnumerable<string> second = codeNames.Skip(4); // Since we only skipped 4 elements, the fifth element // should be in both sequences. IEnumerable<string> concat = first.Concat<string>(second); IEnumerable<string> union = first.Union<string>(second); Console.WriteLine("The count of the codeNames array is: " + codeNames.Count()); Console.WriteLine("The count of the first sequence is: " + first.Count()); Console.WriteLine("The count of the second sequence is: " + second.Count()); Console.WriteLine("The count of the concat sequence is: " + concat.Count()); Console.WriteLine("The count of the union sequence is: " + union.Count()); } }