简体   繁体   English

等于返回意外结果

[英].Equals returning unexpected result

Please see the code below: 请参见下面的代码:

public class ValueType<T> where T : class,new()
    {
        public virtual bool Equals(T other)
        {
            if (other == null)
                return false;
            Type t = GetType();
            Type otherType = other.GetType();
            if (t != otherType)
                return false;
            FieldInfo[] fields = t.GetFields(System.Reflection.BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            foreach (FieldInfo field in fields)
            {
                object value1 = field.GetValue(other);
                object value2 = field.GetValue(this);
                if (value1 == null)
                {
                    if (value2 != null)
                        return false;
                }
                else if (!value1.Equals(value2))
                    return false;
            }
            return true;
        }
    }

    class Tiger : ValueType<Tiger> { public string name; public Tiger mother; }

    class Program
    {
        static void Main(string[] args)
        {
            Tiger t1 = new Tiger() { name = "Teri" }; 
            Tiger t2 = new Tiger() { name = "Teri" }; 
            Tiger t3 = new Tiger() { name = "Toni", mother=t1 };
            Tiger t4 = new Tiger() { name = "Toni", mother = t2 };
            bool Test1 = t4.mother.Equals(t3.mother); //Highlighed line
            bool Test2 = t4.Equals(t3);
        }
    }

I don't understand why the highlighted line returns false. 我不明白为什么突出显示的行返回false。 I would expect it to run in an infinate loop. 我希望它在无限循环中运行。

Why you expect an infinite loop? 为什么会出现无限循环? Following returns true not false : 以下返回true而非false

bool Test1 = t4.mother.Equals(t3.mother); . 

Isn't that expected because both mothers are equal? 难道不是因为两个母亲都平等了吗? They have the same name and there's no "grandmom". 他们有相同的name ,没有“祖母”。 Their mother field returns null since the Tiger is a reference type(class). 由于Tiger是引用类型(类),因此mother字段返回null

Why t4.Equals(t3) returns false ? 为什么t4.Equals(t3)返回false

Because you have declared it as Object , so Object.Equals is used which just compares references. 因为已将其声明为Object ,所以Object.Equals来比较引用。 If you would try-cast it to Tiger this Equals would be called. 如果您将其试铸到Tiger则将称为Equals

That's why this doesn't cause an infinite loop but just returns false (not the same reference): 这就是为什么这不会导致无限循环,而只是返回false (不是相同的引用)的原因:

...else if (!value1.Equals(value2))

Note that you haven't overridden Equals . 请注意,您尚未覆盖Equals You would get your expected behaviour if you'd do it: 如果您愿意,将会得到预期的行为:

class Tiger : ValueType<Tiger> {
    public string name; public Tiger mother;
    public override bool Equals(object obj)
    {
        return ((ValueType<Tiger>) obj).Equals(this);
    }
}

Now this works as expected. 现在,这可以按预期工作。 It won't cause an infinite recursion because at some time the parent mother will be null , but it will check recursively if also the mothers are equal. 它不会引起无限递归,因为在某些时候父母的mother将为null ,但它将递归检查母亲是否相等。

Another way would be to change the signature of the already existing Equals in ValueType : 另一种方法是更改ValueType已经存在的Equals的签名:

public override bool Equals(Object other)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM