namespace MyTestDemo { class MathUtil { /// <summary> /// 获取数组内容的哈希,因为数组的默认哈希值,即使内容相同,数组的默认哈希码也是唯一的 /// </summary> /// <remarks> /// See Jon Skeet (C# MVP) response in the StackOverflow thread /// http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode /// </remarks> /// <param name="array">要取Hash值的数组.</param> /// <returns>当前数组的Hash值</returns> public static int GetHashCode<T>(T[] array) { // if non-null array then go into unchecked block to avoid overflow if (array != null) { unchecked { int hash = 17;
// get hash code for all items in array foreach (var item in array) { hash = hash * 23 + ((item != null) ? item.GetHashCode() : 0); }