LINQ Interview Question #68
What is the difference between Union and Concat?
Joins, Grouping, Set & Quantifier Operators Mid-Level Intermediate
Quick Interview Answer
Union combines two sequences and removes duplicate elements according to equality semantics. Concat appends the second sequence after the first and preserves duplicates.
Detailed Explanation
Union combines two sequences and removes duplicate elements according to equality semantics. Concat appends the second sequence after the first and preserves duplicates.
Choose Union for set-union behavior and Concat when every element from both sequences should remain.
Code Example
var a = new[] { 1, 2, 3 };
var b = new[] { 3, 4 };
var union = a.Union(b); // 1,2,3,4
var concat = a.Concat(b); // 1,2,3,3,4