The SequenceEqual operator

1. What is the SequenceEqual operator?

2. What is the signature of the SequenceEqual operator?

The SequenceEqual operator compares two sequences. If the two sequences have the same number of items and each item in the same position is equal the SequenceEqual operator returns true.

 

public static bool SequenceEqual<TSource>(

    this IEnumerable<TSource> first,

    IEnumerable<TSource> second);

 

public static bool SequenceEqual<TSource>(

    this IEnumerable<TSource> first,

    IEnumerable<TSource> second,

    IEqualityComparer<TSource> comparer);

 

Example 1:

 

            var customers1 = from c in Data.Customers

                             orderby c.Name

                             select c;

 

            var customers2 = from c in Data.Customers

                             orderby c.Name

                             select c;

 

 

            Trace.WriteLine(customers1.SequenceEqual(customers2));

 

            var customers3 = from c in Data.Customers

                             orderby c.Name descending

                             select c;

 

            Trace.WriteLine(customers3.SequenceEqual(customers1));

 

Output:

 

True

False

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s