CSharp - Use custom Comparer with SequenceEqual Operator

Description

Use custom Comparer with SequenceEqual Operator

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from  www . ja  va  2s. c om*/
{
    static void Main(string[] args)
    {
        string[] stringifiedNums1 = { "001", "49", "017", "0080", "00027", "2" };

        string[] stringifiedNums2 = { "1", "0049", "17", "00080", "000000000027", "02" };

        bool eq = stringifiedNums1.SequenceEqual(stringifiedNums2, new MyStringifiedNumberComparer());

        Console.WriteLine(eq);
    }
}

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();
    }
}

Result

Related Topic