How can I create a Singleton class by using public constructor?
11 January 2010
852 views
No Comment
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
- Is it possible to create constructor for an Abstract class?
- Difference between Clone, Close and Dispose methods of Crystal Report?
- Define Lambda expressions
- What is a Lambda expression?
- How do you inherit from a class in c#?












Leave your response!
You must be logged in to post a comment.