Can we have private constructor in our class file. When we
are trying to create instance for the class will it create
or throw error regarding that?
Answer Posted / nevin jain
yes off-course instance can be created...
just make a static function say ReturnInstance() within the
class n keep return type of this function as the name of the
class..
with in ReturnInstance() method return the object of the class..
As the method is static so it will load without using
instance n now we can assign the object(or instance)to the
variable of class type...
examine:
public class A
{
private A()
Console.Writeline("Instance is created");
public static A ReturnInstance()
{
return new A();
}
public void Display()
{
Console.WriteLine("Display is accessed");
}
}
class MyMain
{
public static void main()
{
A a=A.ReturnInstance();
a.Display();
}
}
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
What is array class in c#?
What kind of the information stored inside the assembly?
Is a valid int value?
How do you specify a custom attribute for the entire assembly (rather than for a class)?
Can main method be final?
How does bubble sort work?
What is the purpose of namespace in c#?
How Global.asax is used ?
What are reflections in c#?
What Happens In Memory When You Box And Unbox A Value-type?
What is the difference between method overriding and method overloading?
What is a satellite assembly in c#?
What does question mark mean in c#?
Does unity use c++ or c#?
What do you mean by streamreader/streamwriter class in c#?