From a1749c7e1e45b895bd496f895f39a4df0fef15ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Tue, 23 Jun 2026 08:55:37 +0800 Subject: [PATCH] =?UTF-8?q?ValueRange=20=E7=9B=B8=E5=90=8C=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/General/ValueRange.cs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Shared/General/ValueRange.cs b/Shared/General/ValueRange.cs index 8b31d57..42d1ed7 100644 --- a/Shared/General/ValueRange.cs +++ b/Shared/General/ValueRange.cs @@ -7,7 +7,7 @@ public sealed class ValueRange( T? lower, T? upper, bool isLowerInclusive, bool isUpperInclusive, bool hasLower, bool hasUpper -) where T : notnull, IComparable { +) : IEquatable?> where T : notnull, IComparable { #region 基础字段 @@ -186,4 +186,33 @@ public sealed class ValueRange( return new(lower, upper, isLowerInclusive, isUpperInclusive, anyHasLower, anyHasUpper); } + #region 相等判断(自动生成) + + public override bool Equals(object? obj) => Equals(obj as ValueRange); + public bool Equals(ValueRange? other) { + return other is not null&& + EqualityComparer.Default.Equals(Lower, other.Lower)&& + EqualityComparer.Default.Equals(Upper, other.Upper)&& + HasLower==other.HasLower&& + HasUpper==other.HasUpper&& + IsLowerInclusive==other.IsLowerInclusive&& + IsUpperInclusive==other.IsUpperInclusive; + } + public override int GetHashCode() { + int hashCode = 169504101; + hashCode=hashCode*-1521134295+EqualityComparer.Default.GetHashCode(Lower); + hashCode=hashCode*-1521134295+EqualityComparer.Default.GetHashCode(Upper); + hashCode=hashCode*-1521134295+HasLower.GetHashCode(); + hashCode=hashCode*-1521134295+HasUpper.GetHashCode(); + hashCode=hashCode*-1521134295+IsLowerInclusive.GetHashCode(); + hashCode=hashCode*-1521134295+IsUpperInclusive.GetHashCode(); + return hashCode; + } +#pragma warning disable CS8604 // 引用类型参数可能为 null。 + public static bool operator ==(ValueRange? left, ValueRange? right) => EqualityComparer>.Default.Equals(left, right); +#pragma warning restore CS8604 // 引用类型参数可能为 null。 + public static bool operator !=(ValueRange? left, ValueRange? right) => !(left==right); + + #endregion + } \ No newline at end of file