Return to Snippet

Revision: 56904
at April 20, 2012 17:29 by fraricce


Initial Code
public sealed class MySingleton
	{
		private static MySingleton instance;
		private static readonly Object sync = new object();

		private MySingleton()
		{
			// initialize members here
		}

		public static MySingleton Instance
		{
			get
			{
				if (instance == null)
				{
					lock (sync)
					{
						if (instance == null)
							instance = new MySingleton();
					}
				}

				return instance;
			}
		}
		
		public void SayHello()
		{
			Console.WriteLine("Hello!");
		}
		
	}

Initial URL

                                

Initial Description
Here's an implementation of a thread safe singleton

Initial Title
Thread-safe singleton pattern in C#

Initial Tags

                                

Initial Language
C#