Singleton and thread-safe

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

I am trying to implement a singleton pattern as follows:

public sealed class Singleton
{
private static readonly Singleton only = new Singleton() ;
public static Singleton Instance
{
get
{
return only ;
}
}
..... rest of implementation.....
}

Now my question is if two threads access Singleton.Instance method
simultaneously is there a possibility of creating 2 instances of Singleton
class?

Why or why not?

Thanks!!
 
Hi,
First of all it is static method so there is no need to create instance of
class.Also you must add one private constructor to this class so that no one
can create instance of the class.

Thanksand Regards,
manish bafna
 
Back
Top