| Next Tip?
Home » CSharp, Microsoft .Net

How can I create a Singleton class by using public constructor?

11 January 2010 852 views No Comment
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 2.00 out of 5)
Loading ... Loading ...

I have also asked a tricky question like this:

Can we create a singleton class using public constructor?

Simply answer is yes.

Practically, many of us aware that we can create a singleton class by using private constructor an creating a property, but we can create a singleton class by using public constructor as folows:

static void Main(string[] args)
{
try
{
Singleton obj3 = new Singleton2();
Singleton obj4 = new Singleton2();
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
}

static void Main(string[] args)

{

try

{

Singleton obj3 = new Singleton2();

Singleton obj4 = new Singleton2();

Console.Read();

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

Console.Read();

}

}

Popularity: 15% [?]

Related interview questions

Leave your response!

You must be logged in to post a comment.