It's easy to forget tools we don't use often. I recently had the opportunity to break out a couple of under-utilized LINQ extensions, Intersect
and Except
.
Both do exactly what you'd expect, but let's see them in action.
var primaryColors = new []{"red", "yellow", "blue"};
var secondaryColors = new []{"orange", "green", "purple"};
var patrioticColors = new []{"red", "white", "blue"};
// which colors are both primary colors and patriotic?
var intersection = primaryColors.Intersect(patrioticColors);
// intersection = red, blue
// which colors are primary, but not patriotic?
var exclusions = primaryColors.Except(patrioticColors);
// exclusions = yellow
Under the hood, these methods use an equality comparer to check the hash between values.
Like this article?
0
Software Development Nerd