Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace singleton
- {
- class Program
- {
- static void Main(string[] args)
- {
- Singleton s1 = Singleton.GetInstance();
- Singleton s2 = Singleton.GetInstance();
- Console.WriteLine(s2.ToString());
- Console.WriteLine(s1.ToString());
- Console.ReadLine();
- }
- }
- // NOT THREADSAFE and it is LAZY
- class Singleton
- {
- static Singleton instance;
- // single, STATIC entry point
- public static Singleton GetInstance()
- {
- if (instance == null)
- instance = new Singleton();
- return instance;
- }
- int _test;
- public int test
- {
- get { return _test; }
- set { _test = value; }
- }
- private Singleton()
- {
- test = new Random().Next(1000);
- }
- public override string ToString()
- {
- return test.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement