相关疑难解决方法(0)

重写System.Object.GetHashCode的最佳算法是什么?

在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?

.net algorithm hashcode gethashcode

1389
推荐指数
14
解决办法
19万
查看次数

为什么在重写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万
查看次数

使用Except比较2 List

我有2个List<Animal>我想比较并找到2个List<Animal>对象之间的区别.

Animal object包含以下属性.

ID

名称

年龄

List中list1有10个Animal对象,其中list2有10个Animal对象.在这2个列表中有2个项目(Animal对象相同)

当我使用该Except函数时,我希望remainingList它只包含2列表中不常见的对象.但是,remainingList包含一个副本list1.

我怎么解决这个问题?

 List<Animal> remainingList = list1.Except(list2).toListAsync();
Run Code Online (Sandbox Code Playgroud)

.net c# linq

0
推荐指数
1
解决办法
86
查看次数

标签 统计

.net ×2

c# ×2

hashcode ×2

algorithm ×1

gethashcode ×1

linq ×1

overriding ×1