Creating a unique hash code based on some properties of an object
本问题已经有最佳答案,请猛点这里访问。
我必须上以下课:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class NominalValue { public int Id {get; set;} public string ElementName {get; set;} public decimal From {get; set;} public decimal To {get; set;} public bool Enable {get; set;} public DateTime CreationDate {get; set;} public int StandardValueId {get; set;} //FK for StandardValue } public class StandardValue { public int Id {get; set;} public string ElementName {get; set;} public decimal From {get; set;} public decimal To {get; set;} public bool Enable {get; set;} public DateTime CreationDate {get; set;} } |
用户希望填充
有时我需要知道指定元素的
我不想从
1 2 3 4 5 6 7 8 9 10 11 | public class NominalValue { public int Id {get; set;} public string ElementName {get; set;} public decimal From {get; set;} //<-- 1st parameter for generating hash value public decimal To {get; set;} //<-- 2nd parameter for generating hash value public bool Enable {get; set;} //<-- 3rd parameter for generating hash value public DateTime CreationDate {get; set;} public int StandardValueId {get; set;} public string HashValue {get;set;} //<-- this property added } |
当用户使用
是否有基于3个属性值(
您可以尝试这样做:
1 2 3 4 5 6 7 8 9 10 11 | 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; } } |
也可以对匿名类型使用gethashcode方法
1 2 3 |