Skip to content

Commit 71b9e44

Browse files
authored
Implement GetHashCode() via HashCode.Combine() (#268)
1 parent 19d842c commit 71b9e44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+127
-416
lines changed

Extensions/Xtensive.Orm.Security/Permission.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,7 @@ public override bool Equals(object obj)
5656
}
5757

5858
/// <inheritdoc/>
59-
public override int GetHashCode()
60-
{
61-
unchecked {
62-
int result = (Type != null ? Type.GetHashCode() : 0);
63-
result = (result*397) ^ CanRead.GetHashCode();
64-
result = (result*397) ^ CanWrite.GetHashCode();
65-
result = (result*397) ^ (Query != null ? Query.GetHashCode() : 0);
66-
return result;
67-
}
68-
}
59+
public override int GetHashCode() => HashCode.Combine(Type, CanRead, CanWrite, Query);
6960

7061
#endregion
7162

Orm/Xtensive.Orm.Tests.Core/Caching/CachePerformanceTest.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public override bool Equals(object obj)
5252
return Equals(obj as Item);
5353
}
5454

55-
public override int GetHashCode()
56-
{
57-
return (Value!=null ? Value.GetHashCode() : 0);
58-
}
55+
public override int GetHashCode() => Value?.GetHashCode() ?? 0;
5956

6057
public override string ToString()
6158
{
@@ -174,4 +171,4 @@ private void FetchTest(int count)
174171
}
175172
}
176173
}
177-
}
174+
}

Orm/Xtensive.Orm.Tests.Core/Caching/WeakestCacheTest.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ public override bool Equals(object obj)
3636
return Equals(obj as Item);
3737
}
3838

39-
public override int GetHashCode()
40-
{
41-
return (Value != null ? Value.GetHashCode() : 0);
42-
}
39+
public override int GetHashCode() => Value?.GetHashCode() ?? 0;
4340

4441
public override string ToString()
4542
{
@@ -197,4 +194,4 @@ public void ProfileTest()
197194
}
198195
}
199196
}
200-
}
197+
}

Orm/Xtensive.Orm.Tests.Core/Modelling/IndexingModel/TypeInfo.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,7 @@ public override bool Equals(object obj)
100100
}
101101

102102
/// <inheritdoc/>
103-
public override int GetHashCode()
104-
{
105-
unchecked {
106-
int result = (Type!=null ? Type.GetHashCode() : 0);
107-
result = (result * 397) ^ (IsNullable ? 1 : 0);
108-
result = (result * 397) ^ Length;
109-
result = (result * 397) ^ Scale;
110-
result = (result * 397) ^ Precision;
111-
if (Culture!=null)
112-
result = (result * 397) ^ Culture.GetHashCode();
113-
return result;
114-
}
115-
}
103+
public override int GetHashCode() => HashCode.Combine(Type, IsNullable, Length, Scale, Precision, Culture);
116104

117105
/// <summary>
118106
/// Implements the operator ==.

Orm/Xtensive.Orm.Tests.Framework/Dynamic.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,13 @@ internal class Signature : IEquatable<Signature>
219219
public Signature(IEnumerable<DynamicProperty> properties)
220220
{
221221
this.properties = properties.ToArray();
222-
hashCode = 0;
222+
HashCode hc = new();
223223
foreach (DynamicProperty p in properties)
224224
{
225-
hashCode ^= p.Name.GetHashCode() ^ p.Type.GetHashCode();
225+
hc.Add(p.Name);
226+
hc.Add(p.Type);
226227
}
228+
hashCode = hc.ToHashCode();
227229
}
228230

229231
#region IEquatable<Signature> Members
@@ -241,10 +243,7 @@ public bool Equals(Signature other)
241243

242244
#endregion
243245

244-
public override int GetHashCode()
245-
{
246-
return hashCode;
247-
}
246+
public override int GetHashCode() => hashCode;
248247

249248
public override bool Equals(object obj)
250249
{
@@ -2298,4 +2297,4 @@ internal static class Res
22982297
public const string CloseBracketOrCommaExpected = "']' or ',' expected";
22992298
public const string IdentifierExpected = "Identifier expected";
23002299
}
2301-
}
2300+
}

Orm/Xtensive.Orm.Tests/Linq/LocalCollectionsTest.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public override bool Equals(object obj)
5454
return Equals((Poco<T>) obj);
5555
}
5656

57-
public override int GetHashCode()
58-
{
59-
return Value.GetHashCode();
60-
}
57+
public override int GetHashCode() => Value.GetHashCode();
6158
}
6259

6360
public class Poco<T1, T2>
@@ -85,12 +82,7 @@ public override bool Equals(object obj)
8582
return Equals((Poco<T1, T2>) obj);
8683
}
8784

88-
public override int GetHashCode()
89-
{
90-
unchecked {
91-
return (Value1.GetHashCode() * 397) ^ Value2.GetHashCode();
92-
}
93-
}
85+
public override int GetHashCode() => HashCode.Combine(Value1, Value2);
9486

9587
public Poco(T1 Value1, T2 Value2)
9688
{
@@ -137,15 +129,7 @@ public override bool Equals(object obj)
137129
return Equals((Poco<T1, T2, T3>) obj);
138130
}
139131

140-
public override int GetHashCode()
141-
{
142-
unchecked {
143-
int result = Value1.GetHashCode();
144-
result = (result * 397) ^ Value2.GetHashCode();
145-
result = (result * 397) ^ Value3.GetHashCode();
146-
return result;
147-
}
148-
}
132+
public override int GetHashCode() => HashCode.Combine(Value1, Value2, Value3);
149133

150134
public Poco()
151135
{
@@ -942,4 +926,4 @@ private IEnumerable<Poco<int, decimal, string>> GetLocalItems(int count)
942926
.ToList();
943927
}
944928
}
945-
}
929+
}

Orm/Xtensive.Orm/Annotations.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ public override bool Equals(object obj)
7575
/// Returns the hash code for this instance.
7676
/// </summary>
7777
/// <returns>A hash code for the current <see cref="LocalizationRequiredAttribute"/>.</returns>
78-
public override int GetHashCode()
79-
{
80-
return base.GetHashCode();
81-
}
78+
public override int GetHashCode() => base.GetHashCode();
8279
}
8380

8481
/// <summary>
@@ -521,4 +518,4 @@ public PathReferenceAttribute([PathReference] string basePath)
521518

522519
[UsedImplicitly] public string BasePath { get; private set; }
523520
}
524-
}
521+
}

Orm/Xtensive.Orm/Arithmetic/ArithmeticRules.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ public override bool Equals(object obj) =>
4545
obj is ArithmeticRules other && Equals(other);
4646

4747
/// <inheritdoc/>
48-
public override int GetHashCode()
49-
{
50-
return ((byte)overflowBehavior << 8) | (byte)nullBehavior;
51-
}
48+
public override int GetHashCode() => ((byte) overflowBehavior << 8) | (byte) nullBehavior;
5249

5350

5451
// Constructors

Orm/Xtensive.Orm/Caching/WeakestCache.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ public override bool Equals(object obj)
101101
return Equals((WeakEntry) obj);
102102
}
103103

104-
public override int GetHashCode()
105-
{
106-
return hashCode;
107-
}
104+
public override int GetHashCode() => hashCode;
108105

109106
#endregion
110107

Orm/Xtensive.Orm/Collections/FlagCollection.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,7 @@ public override bool Equals(object obj)
333333
}
334334

335335
/// <inheritdoc/>
336-
public override int GetHashCode()
337-
{
338-
int result = keys.GetHashCode();
339-
result = 29*result + flags.GetHashCode();
340-
return result;
341-
}
336+
public override int GetHashCode() => HashCode.Combine(keys, flags);
342337

343338
#endregion
344339

@@ -407,4 +402,4 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
407402

408403
#endregion
409404
}
410-
}
405+
}

0 commit comments

Comments
 (0)