Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

SingletonProject

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Singleton SingletonObject = Singleton.GetObject();
            SingletonObject.Print("How World");
            Singleton SingletonObject1 = Singleton.GetObject();
            SingletonObject1.Print("Hi World");
               Singleton SingletonObject2 = Singleton.GetObject();
            SingletonObject2.Print("Code World");
            Console.ReadLine();
        }
    }

    public class Singleton
    {
        protected static Singleton _obj ;
  
        private Singleton()
        {
 
        }
        public static Singleton GetObject()
        {
            
            if (_obj == null)
            {
                Console.WriteLine(5);
                _obj = new Singleton();
            }
            if (_obj != null)
            {
            Console.WriteLine(10);
            
            }
            return _obj;
            
            
        }
        public void Print(string s)
        {
            Console.WriteLine(s);
        }
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.