From 2b016fb06a63c658f4dde0e1357188f971559a0b Mon Sep 17 00:00:00 2001 From: Andrei Sergeev Date: Sun, 5 Jan 2025 18:33:50 +0400 Subject: [PATCH] Improve/simplify Optional EqualityComparer GetHashCode impl --- .../Optional.T.EqualityComparer.Equality.cs | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/core-taggeds-optional/Optional/Optional.T.EqualityComparer/Optional.T.EqualityComparer.Equality.cs b/src/core-taggeds-optional/Optional/Optional.T.EqualityComparer/Optional.T.EqualityComparer.Equality.cs index 09131dcc..bc66e30a 100644 --- a/src/core-taggeds-optional/Optional/Optional.T.EqualityComparer/Optional.T.EqualityComparer.Equality.cs +++ b/src/core-taggeds-optional/Optional/Optional.T.EqualityComparer/Optional.T.EqualityComparer.Equality.cs @@ -1,6 +1,4 @@ -using System.Runtime.CompilerServices; - -namespace System; +namespace System; partial struct Optional { @@ -22,19 +20,14 @@ public bool Equals(Optional x, Optional y) } public int GetHashCode(Optional obj) - => - obj.hasValue ? PresentHashCode(obj.value) : AbsentHashCode(); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int PresentHashCode(T value) - => - value is not null - ? HashCode.Combine(true, comparer.GetHashCode(value)) - : HashCode.Combine(true); + { + if (obj.hasValue) + { + return HashCode.Combine( + obj.value is null ? default : comparer.GetHashCode(obj.value)); + } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int AbsentHashCode() - => - HashCode.Combine(false); + return default; + } } }