Creating a unique hash code based on some properties of an object

This question already has an answer here:

  • What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers

  • You can try to do something like this :

    private int GetHashValue() {
        unchecked 
        {
            int hash = 17;
            //dont forget nullity checks 
            hash = hash * 23 + From.GetHashCode();
            hash = hash * 23 + To.GetHashCode();
            hash = hash * 23 + Enable.GetHashCode();
            return hash;
        }
    }
    

    You can use the GetHashCode method on an anonymous type too

    private int GetHashValue() {
       return new { From, To, Enable }.GetHashCode();
    }
    
    链接地址: http://www.djcxy.com/p/39766.html

    上一篇: 使用字节数组作为字典键

    下一篇: 根据对象的某些属性创建唯一的哈希码