使用Hash Code-Concept困境进行对象比较

时间:2015-08-10 12:27:56

标签: c#

我编写了一段代码来检查对象平等。 我从堆栈溢出本身的一个问题中获取了参考。 现在这个代码即使我们有两个不同的对象也是如此。 有人可以解释原因吗?

using System;
namespace ConsolePractice
{
    public class Test
    {
        public int Value { get; set; }
        public string String1 { get; set; }
        public string String2 { get; set; }



        public override int GetHashCode()
        {
            int hash = 19;
            hash = hash * 31 + Value;
            hash = hash * 31 + String1.SafeGetHashCode();
            hash = hash * 31 + String2.SafeGetHashCode();
            return hash;
        }
        public override bool Equals(object obj)
        {
            Test test = obj as Test;
            if (obj == null)
            {
                return false;
            }
            return Value == test.Value &&
                String1 == test.String1 &&
                String2 == test.String2;
        }
    }

    class Demo
    {
        static void Main()
        {
            Test p1 = new Test
            {
                Value = 10,
                String1 = "Test1",
                String2 = "Test2"
            };
            Test p2 = new Test
            {
                Value = 10,
                String1 = "Test1",
                String2 = "Test2"
            };
            bool areEqual = p1.Equals(p2);

            Console.WriteLine(areEqual.ToString());
            Console.ReadLine();

        }
    }
}

并在我的UtilityClass

 static class utility
    {
        public static int SafeGetHashCode<T>(this T value) where T : class
        {
            return value == null ? 0 : value.GetHashCode();
        }
    }

在没有成功之后,我尝试下面的代码也返回true。 我在这做什么大错?请帮忙

using System;

using System.Collections.Generic;


class ThingEqualityComparer : IEqualityComparer<Thing>
{
    public bool Equals(Thing x, Thing y)
    {
        if (x == null || y == null)
            return false;

        return (x.Id == y.Id && x.Name == y.Name);
    }

    public int GetHashCode(Thing obj)
    {
        return obj.GetHashCode();
    }
}


public class Thing
{
    public int Id { get; set; }
    public string Name { get; set; }

}
class Demo
{
    static void Main()
    {
        Thing p1 = new Thing
        {
            Id = 10,
            Name = "Test1",

        };
        Thing p2 = new Thing
        {
            Id = 10,
            Name = "Test1",

        };

        var comparer = new ThingEqualityComparer();
        Console.WriteLine(comparer.Equals(p1, p2));


        Console.ReadLine();

    }
}

1 个答案:

答案 0 :(得分:3)

您可以覆盖Equals()GetHashCode()来定义&#34;等于&#34;意味着在特定的背景下。

你的Equals

Test test = obj as Test;
if (obj == null)
{
  return false;
}
return Value == test.Value &&
  String1 == test.String1 &&
  String2 == test.String2;

有一个错误,它应该是if(test == null) return false;,否则它会说&#34;如果两个Test对象具有相同的ValueString1,则它们是相同的和String2,否则他们不是。您的GetHashCode()与此一致。

因此,让代码返回true不是错误:

  

现在,即使我们有两个不同的对象,这段代码也是真的。

是的,两个不同的相等的对象。

如果您希望EqualsGetHashCode()告诉您它们是否是相同对象(也就是说,类的对象只能等于然后,根本不要覆盖EqualsGetHashCode;保持默认行为。