相关疑难解决方法(0)

为什么在重写Equals方法时重写GetHashCode很重要?

鉴于以下课程

public class Foo
{
    public int FooId { get; set; }
    public string FooName { get; set; }

    public override bool Equals(object obj)
    {
        Foo fooItem = obj as Foo;

        if (fooItem == null) 
        {
           return false;
        }

        return fooItem.FooId == this.FooId;
    }

    public override int GetHashCode()
    {
        // Which is preferred?

        return base.GetHashCode();

        //return this.FooId.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经覆盖了该Equals方法,因为它Foo代表了Foos表的一行.哪个是覆盖的首选方法GetHashCode

覆盖为什么重要GetHashCode

c# overriding hashcode

1371
推荐指数
13
解决办法
35万
查看次数

VB.NET中的显式接口实现

如何在VB.NET中实现显式接口实现?

.net vb.net

13
推荐指数
3
解决办法
3576
查看次数

为什么Assert.AreEqual <T>失败并且具有相同的对象?

我有一个包含这个属性的类:

public class Person
{
    public long Id { get; set; }

    public string Name { get; set; }

    public int? IdCountry { get; set; }
    public virtual Country Country { get; set; }
    public int? IdState { get; set; }
    public virtual State State { get; set; }
}

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

public class State
{
    public int Id { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# assert

3
推荐指数
1
解决办法
459
查看次数

标签 统计

c# ×2

.net ×1

assert ×1

hashcode ×1

overriding ×1

vb.net ×1