using System.Runtime.CompilerServices; namespace System.Collections.Generic { partial class Comparer { static volatile Comparer defaultComparer; public static Comparer Default { get { Comparer comparer = defaultComparer; if (comparer == null) { comparer = CreateComparer(); defaultComparer = comparer; } return comparer; } } static Comparer CreateComparer() { RuntimeType t = (RuntimeType)typeof(T); if (typeof(IComparable).IsAssignableFrom(t)) return (Comparer)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(GenericComparer<>), t); // If T is a Nullable where U implements IComparable return a NullableComparer if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) { RuntimeType u = (RuntimeType)t.GetGenericArguments()[0]; if (typeof(IComparable<>).MakeGenericType (u).IsAssignableFrom (u)) return (Comparer)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(NullableComparer<>), u); } if (t.IsEnum) return (Comparer)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(EnumComparer<>), t); // Otherwise return an ObjectComparer return new ObjectComparer (); } } partial class EnumComparer { [MethodImpl (MethodImplOptions.AggressiveInlining)] public override int Compare (T x, T y) => JitHelpers.EnumCompareTo (x, y); } }