Use SequenceEqual Operator with custom IEqualityComparer in CSharp

Description

The following code shows how to use SequenceEqual Operator with custom IEqualityComparer.

Example


 //from w  ww  .  j a  v a2s  . c o  m
 
 
  

using System;
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);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ