diff --git a/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs b/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs index 988cc218e9fde..d4eb6576b3aed 100644 --- a/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs +++ b/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs @@ -7,16 +7,15 @@ namespace System.Collections { public static class StructuralComparisons { - private static volatile IComparer? s_StructuralComparer; - private static volatile IEqualityComparer? s_StructuralEqualityComparer; + public static IComparer StructuralComparer => System.Collections.StructuralComparer.s_instance; - public static IComparer StructuralComparer => s_StructuralComparer ??= new StructuralComparer(); - - public static IEqualityComparer StructuralEqualityComparer => s_StructuralEqualityComparer ??= new StructuralEqualityComparer(); + public static IEqualityComparer StructuralEqualityComparer => System.Collections.StructuralEqualityComparer.s_instance; } internal sealed class StructuralEqualityComparer : IEqualityComparer { + internal static readonly StructuralEqualityComparer s_instance = new(); + public new bool Equals(object? x, object? y) { if (x != null) @@ -58,6 +57,8 @@ public int GetHashCode(object obj) internal sealed class StructuralComparer : IComparer { + internal static readonly StructuralComparer s_instance = new(); + public int Compare(object? x, object? y) { if (x == null) return y == null ? 0 : -1;