コード例 #1
0
        public static RateSingleton GetInstance()

        {
            //The approach below ensures that only one instance is created and only

            //when the instance is needed. Also, the uniqueInstance variable is

            //declared to be volatile to ensure that assignment to the instance variable

            //completes before the instance variable can be accessed. Lastly,

            //this approach uses a syncRoot instance to lock on, rather than

            //locking on the type itself, to avoid deadlocks.



            if (uniqueInstance == null)

            {
                lock (syncRoot)

                {
                    if (uniqueInstance == null)

                    {
                        uniqueInstance = new RateSingleton();
                    }
                }
            }

            return(uniqueInstance);
        }
コード例 #2
0
        public double GetRate()

        {
            RateSingleton rate = RateSingleton.GetInstance();

            return(rate.CurrentRate);
        }
コード例 #3
0
ファイル: RateSingleton.cs プロジェクト: alannet/example
        public static RateSingleton GetInstance()
        {
            //The approach below ensures that only one instance is created and only
            //when the instance is needed. Also, the uniqueInstance variable is
            //declared to be volatile to ensure that assignment to the instance variable
            //completes before the instance variable can be accessed. Lastly,
            //this approach uses a syncRoot instance to lock on, rather than
            //locking on the type itself, to avoid deadlocks.

            if(uniqueInstance == null)
            {
                lock (syncRoot)
                {
                    if(uniqueInstance == null)
                    {
                        uniqueInstance = new RateSingleton();
                    }
                }
            }
            return uniqueInstance;
        }