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!!Jon Skeet's article:
http://www.yoda.arachsys.com/csharp/singleton.html
Gabriel Lozano-Morn
"Diffident" <Diffident@.discussions.microsoft.com> wrote in message
news:21EE451E-6020-48C8-8334-5E1C6A66AFB7@.microsoft.com...
> 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:
> 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!!
Wednesday, March 28, 2012
Singleton and thread-safe
Labels:
asp,
class,
followspublic,
implement,
net,
pattern,
readonly,
sealed,
singleton,
singleton123private,
static,
thread-safe
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment