Design Patterns-thread-safe Singleton mode (C #)

Source: Internet
Author: User

1, a Hungry man type single case mode

A hungry man singleton mode-by Chimomonamespace csharplearning{public    sealed class Singleton    {        private static readonly Sing Leton instance = new Singleton ();        Private Singleton () {} public        static Singleton Instance        {            get {return Instance;}        }    }

2, lazy single-case mode

2.1, the use of internal class

Lazy Singleton mode: With an internal class-by Chimomonamespace csharplearning{public    sealed class Singleton    {        private Singleton () { }        private Static class Singletonholder        {public            static readonly Singleton instance = new Singleton ();        } Public        static Singleton Instance        {            get {return singletonholder.instance;}}}    
2.2, Ordinary Lock

Lazy single-case mode: normal lock-by chimomonamespace csharplearning{public    sealed class Singleton    {        private static volatile Singleton instance;        private static readonly Object syncRoot = new Object ();        Private Singleton () {} public        static Singleton Instance        {            get            {                lock (syncRoot)                {                    if ( instance = = null)                    {                        instance = new Singleton ();}                }                return instance;}}}    
2.3. Double Detection plus lock

Lazy single-case mode: Dual detection lock-by chimomonamespace csharplearning{public    sealed class Singleton    {        private static volatile Singleton instance;        private static readonly Object syncRoot = new Object ();        Private Singleton () {} public        static Singleton Instance        {            get            {                if (Instance = = null)                {                    Lock (SyncRoot)                    {                        if (instance = = null)                        {                            instance = new Singleton ();                }}} return instance;}}}    

Design Patterns-thread-safe Singleton mode (C #)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.