0% found this document useful (0 votes)
6 views2 pages

C# Singleton Pattern Implementation

Uploaded by

tuandq.22ite
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

C# Singleton Pattern Implementation

Uploaded by

tuandq.22ite
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

using System;

class Singleton

private static Singleton _instance;

private static readonly object _lock = new object();

private Singleton()

[Link]("Singleton instance được tạo.");

public static Singleton GetInstance()

if (_instance == null)

lock (_lock)

if (_instance == null)

_instance = new Singleton();

}
}

return _instance;

public void ShowMessage(string message)

[Link]($"Message từ Singleton: {message}");

class Program

static void Main(string[] args)

Singleton instance1 = [Link]();

[Link]("Xin chào từ instance1!");

Singleton instance2 = [Link]();

[Link]("Xin chào từ instance2!");

[Link]($"instance1 và instance2 có giống nhau không? {ReferenceEquals(instance1,


instance2)}");

You might also like