SequenceEqual operator Part 10

Sequence operator determines if two sequences are equal to each other.Two collections are considered equal if they have the same number of elements with same values and in present in the same order.

The prototype of Sequence looks like this

public static bool SequenceEqual<T>(
this IEnumerable<T> first,
IEnumerable<T> second);

 

SequenceEqual query operator is an extension method that is available on objects that implement IEnumerable<T>. SequenceEqual operator determines if two sequences are equal to each other.It enumerates through each sequence in parallel and compares each element by calling its Equal method to determine if they are equal. If the sequence are equal, the return value is true otherwise false. Let's see few examples of SequenceEqual to see its usage.

image

 

CWindowssystem32cmd.exe

In the above example, I have tried to cover all the possible combinations where two sequence may be equal or not equal to each other.

The first example is fairly simple where I test if list1 and list2 are equal each other. Since list1 and list2 are equal to each other, the output confirms that list1 and list2 are equal to each other.

In the second example where I make use of list3 and list4, I have modified list4 from list3 by changing the order of the elements. Therefore when I compare list3 and list4, the output shows that list3 and list4 are not equal. This confirms the fact SequenceEqual requires elements in both sequences must be in the same order.

In the third example where I make use of list5 and list6, I have modified list6 from list5 by adding another element. When you run the query, the result shows that list5 and lis6 are not equal. The reason is, Sequence equal also requires that both list have the same number of elements which in the cast of list6 had an extra element resulting in false value.

So far we have compared list which have values types, what happens when our collection contains complex types like students. How would you use SequenceEqual in that case and how would SequenceEqual know if two students are equal. This is where the second overloaded version of the SequenceEqual comes handy.  This is how the second prototype for SequenceEqual looks like

public static bool SequenceEqual<T>(
this IEnumerable<T> first,
IEnumerable<T> second,
IEqualityComparer<T> comparer);

As I mentioned earlier, by default sequence equals calls equals method on two elements to determine if they are equal. However in the case of complex type we can pass in a custom IEqualtiyComparer and specify how two students are considered equal. For the demonstration purposes, I have made a rule that if the student's lastname is same, it is considered the same student. Let's see an example of that.

This is how my complex Student object and StudentEqualityComparer looks like.

image

 

image

CWindowssystem32cmd.exe

 

In the above example I am making use of a complex type called Student which has two properties firstname and lastname. In the list1 and list2 I have two students. The common item in both the list is that both student's lastnames are same in both list. By default both list are different from each other, but I make use of StudentComparer which defines the rule that if a student's lastname is same then it should be considered the same student. Therefore when you run the example the output returns true.

No Comments