1+
2+ // This file provides alternative implementation of the HashCode.Combine{T1}(T1) method for
3+ // legacy .NET Framework and earlier versions of .NET Coree (below .NET 8).
4+
5+ #if NET8_0_OR_GREATER
6+ using System ;
7+
8+ namespace IGLib . Base
9+ {
10+
11+ /// <summary>Contains methods that combines hashes of multipe objects into a single hash.
12+ /// <para>In .NET 8 or greater, this just calls <see cref="HashCode.Combine{T1}(T1)"/>.
13+ /// </para></summary>
14+ public static class HashCodeHelper
15+ {
16+ public static int Combine < T1 > ( T1 value1 )
17+ {
18+ return HashCode . Combine ( value1 ) ;
19+ }
20+
21+ public static int Combine < T1 , T2 > ( T1 value1 , T2 value2 )
22+ {
23+ return HashCode . Combine ( value1 , value2 ) ;
24+ }
25+
26+ public static int Combine < T1 , T2 , T3 > ( T1 value1 , T2 value2 , T3 value3 )
27+ {
28+ return HashCode . Combine ( value1 , value2 , value3 ) ;
29+ }
30+
31+ // Add more overloads as needed
32+ }
33+ }
34+ #else
35+ using System ;
36+
37+ namespace IGLib . Base
38+ {
39+
40+ /// <summary>Contains methods that combines hashes of multipe objects into a single hash.
41+ /// <para>In .NET 8 or greater, this just calls `HashCode.Combine{T1}(T1)`, but for lower
42+ /// versions (including .NET Framework), the class provides its own implementation.
43+ /// </para></summary>
44+ public static class HashCodeHelper
45+ {
46+ public static int Combine < T1 > ( T1 value1 )
47+ {
48+ return value1 ? . GetHashCode ( ) ?? 0 ;
49+ }
50+
51+ public static int Combine < T1 , T2 > ( T1 value1 , T2 value2 )
52+ {
53+ int hash1 = value1 ? . GetHashCode ( ) ?? 0 ;
54+ int hash2 = value2 ? . GetHashCode ( ) ?? 0 ;
55+
56+ return CombineHashes ( hash1 , hash2 ) ;
57+ }
58+
59+ public static int Combine < T1 , T2 , T3 > ( T1 value1 , T2 value2 , T3 value3 )
60+ {
61+ int hash1 = value1 ? . GetHashCode ( ) ?? 0 ;
62+ int hash2 = value2 ? . GetHashCode ( ) ?? 0 ;
63+ int hash3 = value3 ? . GetHashCode ( ) ?? 0 ;
64+
65+ return CombineHashes ( hash1 , hash2 , hash3 ) ;
66+ }
67+
68+ // Add more overloads as needed
69+
70+ private static int CombineHashes ( params int [ ] hashes )
71+ {
72+ int hash = 17 ;
73+
74+ foreach ( int h in hashes )
75+ {
76+ hash = hash * 31 + h ;
77+ }
78+
79+ return hash ;
80+ }
81+ }
82+ }
83+ #endif
0 commit comments