Is this Comparer class from SSCLI ThreadSafe library code?

public abstract class Comparer<T> : IComparer, IComparer<T>
    {
        static Comparer<T> defaultComparer;    

        public static Comparer<T> Default {
            get {
                Comparer<T> comparer = defaultComparer;
                if (comparer == null) {
                    comparer = CreateComparer();
                    defaultComparer = comparer;
                }
                return comparer;
            }
        }

First, is the default property stream safe? Is it not possible that the effect of the following statement

comparer = CreateComparer(); 

maybe not visible to threads other than creating a thread? So, are several instances of Comparer built?

Is this Microsoft in order to dissolve the cost of synchronizing with the creation of multiple objects?

Secondly, why is defaultComparer first assigned to a matching variable ... and then replaced later? Why Comparer comparer = defaultComparer?

+3
source share
2 answers

. , defaultComparer . , . . , CLR , , .

+1

, , . , Default , , , , . , , , . , , , - , , , .

, , , , IComparer . , , , . , , Default .

0

All Articles