Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

SequenceEqual Operator

Modified: 2007/01/20 04:44 by t_magennis - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

Compares two sequences and returns true only if the members values, order and count are identical. (This operated was new in the January CTP, it is identical to the EqualAll Operator that was in previous CTP's).

Edit

Method Signatures

// 1 - Return true if the two sequences are equal in values, order and count.
public static bool SequenceEqual<TSource>(
    this IEnumerable<TSource> first, 
    IEnumerable<TSource> second)
 
// 2 - Return true if the two sequences are equal in values, order and count.
// Allows you to specify a custom comparer function.
public static bool SequenceEqual<TSource>(
    this IEnumerable<TSource> first, 
    IEnumerable<TSource> second, 
    IEqualityComparer<TSource> comparer)



Edit

Exceptions

Throws an ArgumentNullException if first or second is null.


Edit

Pseudo-code

Overload 1
If first is null, throw an ArgumentNullException.
If second is null, throw an ArgumentNullException.


Begin iterating the first and second sequences.
Get the next (or first) first element.
Get the next (or first) second element.
If there is no next second element, return false.
Compare the current first and second element using EqualityComparer<TSource>.Default.
If they are NOT equal, return false.

If there are any remaining second elements (more than the first sequence has), return false.
Else, return true. The sequence elements are equal in value, order and length.


Overload 2
If first is null, throw an ArgumentNullException.
If second is null, throw an ArgumentNullException.


Begin iterating the first and second sequences.
Get the next (or first) first element.
Get the next (or first) second element.
If there is no next second element, return false.
Compare the current first and second element using the comparer equality comparer function.
If they are NOT equal, return false.

If there are any remaining second elements (more than the first sequence has), return false.
Else, return true. The sequence elements are equal in value, order and length.


Edit

Loop Count

2 if true, < 2 if false. Iterates through both the first and second sequences. Will return false at the first difference.


Edit

Code Samples

TODO: Needs code sample.

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.