diff --git a/src/libraries/System.Private.CoreLib/src/System/Delegate.cs b/src/libraries/System.Private.CoreLib/src/System/Delegate.cs index d24059770d5f88..403d6eac3ae9d3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Delegate.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Delegate.cs @@ -164,14 +164,12 @@ public bool MoveNext() [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Delegate? d1, Delegate? d2) { - // Test d2 first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (d2 is null) + if (ReferenceEquals(d1, d2)) { - return d1 is null; + return true; } - return ReferenceEquals(d2, d1) ? true : d2.Equals((object?)d1); + return d2 is not null && d2.Equals(d1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -179,12 +177,12 @@ public bool MoveNext() { // Test d2 first to allow branch elimination when inlined for not null checks (!= null) // so it can become a simple test - if (d2 is null) + if (ReferenceEquals(d1, d2)) { - return d1 is not null; + return false; } - return ReferenceEquals(d2, d1) ? false : !d2.Equals(d1); + return d2 is not null && !d2.Equals(d1); } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/SortVersion.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/SortVersion.cs index 448a0d1c387354..95f87a8d587b04 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/SortVersion.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/SortVersion.cs @@ -64,12 +64,12 @@ public override int GetHashCode() { // Test "right" first to allow branch elimination when inlined for null checks (== null) // so it can become a simple test - if (right is null) + if (ReferenceEquals(left, right)) { - return left is null; + return true; } - return right.Equals(left); + return right is not null && right.Equals(left); } public static bool operator !=(SortVersion? left, SortVersion? right) => diff --git a/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs b/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs index 2508f2dc09f15a..b1b2962b4502fd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs @@ -28,14 +28,12 @@ protected MulticastDelegate([DynamicallyAccessedMembers(DynamicallyAccessedMembe [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(MulticastDelegate? d1, MulticastDelegate? d2) { - // Test d2 first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (d2 is null) + if (ReferenceEquals(d1, d2)) { - return d1 is null; + return true; } - return ReferenceEquals(d2, d1) ? true : d2.Equals((object?)d1); + return d2 is not null && d2.Equals(d1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -45,12 +43,12 @@ protected MulticastDelegate([DynamicallyAccessedMembers(DynamicallyAccessedMembe // Test d2 first to allow branch elimination when inlined for not null checks (!= null) // so it can become a simple test - if (d2 is null) + if (ReferenceEquals(d1, d2)) { - return d1 is not null; + return false; } - return ReferenceEquals(d2, d1) ? false : !d2.Equals(d1); + return d2 is not null && !d2.Equals(d1); } } #pragma warning restore CS0660, CS0661 diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs index d467fd6fb08242..20b07e73dbc738 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs @@ -187,20 +187,12 @@ public override string ToString() [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Assembly? left, Assembly? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(Assembly? left, Assembly? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs index c4619fdead1218..d54dbd6d103bf2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs @@ -24,20 +24,12 @@ protected ConstructorInfo() { } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(ConstructorInfo? left, ConstructorInfo? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(ConstructorInfo? left, ConstructorInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs index a42feec69e5d5c..c0fe1fad5aadde 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs @@ -87,20 +87,12 @@ public virtual void RemoveEventHandler(object? target, Delegate? handler) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(EventInfo? left, EventInfo? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(EventInfo? left, EventInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs index 374c0ba5aeeb13..bbf3cbac69f785 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs @@ -43,20 +43,12 @@ protected FieldInfo() { } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(FieldInfo? left, FieldInfo? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(FieldInfo? left, FieldInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs index 89cc304a0ef42d..186559e47f7d3c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs @@ -46,20 +46,12 @@ public virtual Module Module [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(MemberInfo? left, MemberInfo? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(MemberInfo? left, MemberInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs index 087f9953a3af4d..8d476aaa2d4458 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs @@ -68,20 +68,12 @@ this is ConstructorInfo && [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(MethodBase? left, MethodBase? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(MethodBase? left, MethodBase? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs index 5e8afa6d70e30d..0c8a39a5817dcd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs @@ -41,20 +41,12 @@ protected MethodInfo() { } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(MethodInfo? left, MethodInfo? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(MethodInfo? left, MethodInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs index ed57d6891e283c..58773e55314f0a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs @@ -148,20 +148,12 @@ public virtual Type[] FindTypes(TypeFilter? filter, object? filterCriteria) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Module? left, Module? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(Module? left, Module? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs index 24c1196334a8b0..c54a1ec27003ac 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs @@ -62,20 +62,12 @@ protected PropertyInfo() { } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(PropertyInfo? left, PropertyInfo? right) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (right is null) - { - return left is null; - } - - // Try fast reference equality and opposite null check prior to calling the slower virtual Equals if (ReferenceEquals(left, right)) { return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(PropertyInfo? left, PropertyInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/FrameworkName.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/FrameworkName.cs index 973a13aafabf2a..9612f33b55eab0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/FrameworkName.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/FrameworkName.cs @@ -197,12 +197,12 @@ public FrameworkName(string frameworkName) public static bool operator ==(FrameworkName? left, FrameworkName? right) { - if (left is null) + if (ReferenceEquals(left, right)) { - return right is null; + return true; } - return left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(FrameworkName? left, FrameworkName? right) diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.cs b/src/libraries/System.Private.CoreLib/src/System/Type.cs index 17e4636296c177..e30fe8086bdb01 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.cs @@ -663,7 +663,6 @@ internal string FormatTypeName() public override string ToString() => "Type: " + Name; // Why do we add the "Type: " prefix? - public override bool Equals(object? o) => o == null ? false : Equals(o as Type); public override int GetHashCode() { Type systemType = UnderlyingSystemType; @@ -671,7 +670,9 @@ public override int GetHashCode() return systemType.GetHashCode(); return base.GetHashCode(); } - public virtual bool Equals(Type? o) => o == null ? false : ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType); + + public override bool Equals(object? o) => o is Type type && Equals(type); + public virtual bool Equals(Type? o) => o is not null && ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType); [Intrinsic] public static bool operator ==(Type? left, Type? right) diff --git a/src/libraries/System.Private.CoreLib/src/System/Version.cs b/src/libraries/System.Private.CoreLib/src/System/Version.cs index 99c26d59a91439..4a9f73c55b6a53 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Version.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Version.cs @@ -390,15 +390,12 @@ private static bool TryParseComponent(ReadOnlySpan component, string compo [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Version? v1, Version? v2) { - // Test "right" first to allow branch elimination when inlined for null checks (== null) - // so it can become a simple test - if (v2 is null) + if (ReferenceEquals(v1, v2)) { - return v1 is null; + return true; } - // Quick reference equality test prior to calling the virtual Equality - return ReferenceEquals(v2, v1) ? true : v2.Equals(v1); + return v2 is not null && v2.Equals(v1); } public static bool operator !=(Version? v1, Version? v2) => !(v1 == v2);