相关疑难解决方法(0)

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

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

.net algorithm hashcode gethashcode

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

使用一个字段在类中覆盖Equals和GetHashCode

我有一节课:

public abstract class AbstractDictionaryObject
    {
        public virtual int LangId { get; set; }

        public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType() != GetType())
            {
                return false;
            }

            AbstractDictionaryObject other = (AbstractDictionaryObject)obj;
            if (other.LangId != LangId)
            {
                return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            int hashCode = 0;               
            hashCode = 19 * hashCode + LangId.GetHashCode();
            return hashCode;
        }
Run Code Online (Sandbox Code Playgroud)

我派出了类:

public class Derived1:AbstractDictionaryObject
{...}

public class Derived2:AbstractDictionaryObject
{...}
Run Code Online (Sandbox Code Playgroud)

在这AbstractDictionaryObject只是一个共同的领域:LangId. …

c# overriding equals gethashcode

2
推荐指数
1
解决办法
1670
查看次数

标签 统计

gethashcode ×2

.net ×1

algorithm ×1

c# ×1

equals ×1

hashcode ×1

overriding ×1