0

I want to create a Singleton for my class, I have the following code, it works fine but is there any potential issue which I am not able to see ?

public class Singleton
    {
        private static Singleton _singleton;
        private Singleton()
        {

        }
        public static Singleton GetInstance()
        {
            Interlocked.CompareExchange(ref _singleton, new Singleton(), null);
            return _singleton;
        }
        public void SayHello()
        {
            Console.WriteLine($" Hello from {Thread.CurrentThread.ManagedThreadId}");
        }
    }
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135

0 Answers0