Skip to content

Commit 2849641

Browse files
committed
Add ArrayHelper.ArrayGetHashCode function.
1 parent 2fc213e commit 2849641

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/NHibernate/Util/ArrayHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,23 @@ public static bool ArrayEquals(byte[] a, byte[] b)
244244
}
245245
return true;
246246
}
247+
248+
/// <summary>
249+
/// Calculate a hash code based on the length and contents of the array.
250+
/// The algorithm is such that if ArrayHelper.ArrayEquals(a,b) returns true,
251+
/// then ArrayGetHashCode(a) == ArrayGetHashCode(b).
252+
/// </summary>
253+
/// <typeparam name="T"></typeparam>
254+
/// <param name="array"></param>
255+
/// <returns></returns>
256+
public static int ArrayGetHashCode<T>(T[] array)
257+
{
258+
int hc = array.Length;
259+
260+
foreach (var e in array)
261+
hc = unchecked(hc*31 + e.GetHashCode());
262+
263+
return hc;
264+
}
247265
}
248266
}

0 commit comments

Comments
 (0)