Singleton and thread-safe

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!!
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top