Implement the Singleton Pattern - CSharp Design Patterns

CSharp examples for Design Patterns:Singleton Pattern

Description

Implement the Singleton Pattern

Demo Code


using System;/* w  w w  .  j av  a 2s  .  com*/

    public class SingletonExample
    {
        private static SingletonExample instance;

        static SingletonExample()
        {
            instance = new SingletonExample();
        }

        private SingletonExample() { }

        public static SingletonExample Instance
        {
            get { return instance; }
        }

        public void SomeMethod1() { /*..*/ }
        public void SomeMethod2() { /*..*/ }
    }

class MainClass
    {
        public static void Main()
        {
            SingletonExample s = SingletonExample.Instance;
            s.SomeMethod1();

            SingletonExample.Instance.SomeMethod2();

        }
    }

Related Tutorials