IEnumerable.Intersect与自定义比较器,不明白行为

Whe*_*ism 3 c# linq

我正在尝试使用a来创建一个IEqualityComparer用于比较字符串的自定义的自定义Regex,而我真的只是不知道它正在做什么.

这是来自LinqPad测试的代码.

比较器:

public class RegexEqualityComparer : EqualityComparer<string>
{
    public RegexEqualityComparer(string pattern, RegexOptions options = RegexOptions.None)
    {
        _re = new Regex(pattern, options);
    }

    public RegexEqualityComparer(Regex re)
    {
        _re = re;
    }

    public override bool Equals(string x, string y)
    {           
        bool res = false;

        if (Object.ReferenceEquals(x, y)) 
            res = true;
        else if (x != null && y != null)
            res = _re.IsMatch(x) && _re.IsMatch(y);

        String.Format("RES: {0}, {1} = {2}", new object[] { x, y, res }).Dump();

        return res;            
    }

    public override int GetHashCode(string obj)
    {
        return obj.GetHashCode();
    }

    // ------------------------------------------------------------------------------------------------------------------------------------------------ 
    private Regex _re;
}
Run Code Online (Sandbox Code Playgroud)

叫:

RegexEqualityComparer comparer = new RegexEqualityComparer(@"^-");

new string[] { "1", "-4" }.Intersect(new string[] { "1", "-" }, comparer).Dump();
Run Code Online (Sandbox Code Playgroud)

我希望这能给我{ "1", "-4" }- 根据相等比较器,set1中的两个元素都出现在set2中; 它实际给出的是:

RES: 1, 1 = True
{ "1" }
Run Code Online (Sandbox Code Playgroud)

让我感到困惑的是,根据Dump()比较器中的LinqPad ,即使尝试将其-4与任何东西进行比较也不会感到困扰- 唯一的转储就是RES: 1, 1 = True

我确定我在这里遗漏了一些明显的东西,但根本看不到它!

Jon*_*nna 6

public override int GetHashCode(string obj)
{
    return obj.GetHashCode();
}
Run Code Online (Sandbox Code Playgroud)

如果Equals(a, b)那时它是必需的GetHashCode(a) == GetHashCode(b).这不是您的担保EqualityComparer,这个错误意味着Intersect()调用找不到匹配的值.

一个明智的实现与您的对应Equals:

public override int GetHashCode(string obj)
{
  if (obj == null) return 0;
  if (_re.IsMatch(obj)) return 1;
  return obj.GetHashCode(); // catch the reference-equals part for non-matches.
}
Run Code Online (Sandbox Code Playgroud)

具有相同字符的两个字符串被认为是不相等的(即它认为new string('1', 1)不同"1")的事实可能是故意的或者可能是错误.也许ReferenceEquals()应该是字符串==