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!!Jon Skeet's article:
http://www.yoda.arachsys.com/csharp/singleton.html
Gabriel Lozano-Morn
"Diffident" <Diffident@.discussions.microsoft.comwrote in message
news:21EE451E-6020-48C8-8334-5E1C6A66AFB7@.microsoft.com...
Quote:
Originally Posted by
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
"Diffident" wrote:
Quote:
Originally Posted by
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!!
0 comments:
Post a Comment