SequenceEqual

In this chapter you will learn:

  1. How to use SequenceEqual operator
  2. SequenceEqual with custom Comparer

Get to know SequenceEqual operator

using System;/* java  2  s  .  c  om*/
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass {
    public static void Main() {
        string[] presidents = {"G", "H", "java2s.com", "H", "over", "Jack"};

        bool eq = presidents.SequenceEqual(presidents);
        Console.WriteLine(eq);

    }
}

SequenceEqual with custom Comparer

using System;/*from   j  a  v a 2  s. com*/
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MyStringifiedNumberComparer : IEqualityComparer<string> {
    public bool Equals(string x, string y) {
        return (Int32.Parse(x) == Int32.Parse(y));
    }

    public int GetHashCode(string obj) {
        return Int32.Parse(obj).ToString().GetHashCode();
    }
}
public class MainClass {
    public static void Main() {
        string[] stringifiedNums1 = { "101", "49", "017", "1080", "00027", "2" };
        string[] stringifiedNums2 = {"1", "1049", "17", "080", "27", "02" };
        bool eq = stringifiedNums1.SequenceEqual(stringifiedNums2,new MyStringifiedNumberComparer());
        Console.WriteLine(eq);
    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use Single operator
  2. SingleOrDefault vs Single
Home » C# Tutorial » Linq Operators
Aggregate
Aggregate with seed
Aggregate string value
All
Any
Average
Cast
Concat
Contains
Count
DefaultIfEmpty
Distinct
ElementAt
ElementAtOrDefault
Empty
Except
FindAll
First
FirstOrDefault
GroupBy
Intersect
Last
LastOrDefault
LongCount
Max
Min
OfType
OrderBy
OrderByDescending
Range
Repeat
Reverse
SelectMany
SequenceEqual
Single
SingleOrDefault
Skip
SkipWhile
Sum
Take
TakeWhile
ThenBy
ThenByDescending
ToArray
ToList
Zip