Wednesday, September 24, 2008

SequenceEqual is my favorite new hammer

A common programming task that many people face is comparing 2 enumerations to determine if they are equal. Ideally you would like to be able to write code like the following:

if(arrayA == arrayB)
{
//Do Stuff Here...
}

However as you know, arrayA will never equal arrayB in the code above since they are completely different objects. What you really want to know is if the values contained in arrayA are the same as the values contained in arrayB. Well, the SequenceEqual() method provides a mechanisim to do just that.

The SequenceEqual() method enumerates the two source sequences in parallel and compares corresponding elements by using the default equality comparer for TSource.

Here is a quick example showing 2 equal arrays...

string[] arrayA = new string[]{"a","b","c","d"};
string[] arrayB = new string[]{"a","b","c","d"};
string[] arrayC = new string[]{"w","x","y","z"};

bool A_Equals_B = arrayA.SequenceEqual(arrayB); //returns true;
bool A_Equals_C = arrayA.SequenceEqual(arrayC); //returns false;

Labels: , ,

Tuesday, July 29, 2008

Cool LINQ Tool

There is a cool LINQ tool called LINQPad available from O'Reilly.

It is pre-loaded with a bunch of great examples from C# 3.0 In a Nutshell by Joseph Albahari and
Ben Albahari

Check it out...

Labels: , , ,

Thursday, May 29, 2008

Neat coding tricks with LINQ

Igor Ostrovsky has compiled a list of 7 tricks to simplify common coding tasks using LINQ.

The LINQ approach to converting sequences or collections is very elegant.

Here are 101 other samples

Labels: , , ,