Powerful Lists

As a part of the infrastructure introduced in .NET 3.5 to support the various flavours of LINQ, there are a whole heap of generic extension methods that are available whenever you have IEnumerable<T>.

Two that I've found useful recently are Intersect() and Except() - both of these work to filter values out of the sequence.

A couple of examples are the best way to understand them:

   1:  
   2: // Returns all multiples of three
   3: IEnumerable<int> multiplesOfThree = ...;
   4: // 3, 6, 9, 12, 15, 18, 21, ...
   5:  
   6: // Returns all multiples of four
   7: IEnumerable<int> multiplesOfFour = ...;
   8: // 4, 8, 12, 16, 20, 24, ...
   9:  
  10: // Returns multiplesOfThree that are NOT multiplesOfFour
  11: IEnumerable<int> exceptExample = multiplesOfThree.Except(multiplesOfFour);
  12: // 3, 6, 9, 15, 18, 21, 27 ...
  13:  
  14: // Returns multiplesOfThree that ARE multiplesOfFour
  15: IEnumerable<int> intersectExample = multiplesOfThree.Intersect(multiplesOfThree);
  16: // 12, 24, 36, 48 ...
  17:  

 

If you haven't already, check out the members of Enumerable on MSDN - well worth the effort.