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
Where is the keyword void used?
What are the benefits of using generics in c#?
Explain about Serialize and MarshalByRef?
Where test director stores its data ? Database ,Local file etc...? I need to read this data from Visual Studio 2005 c# client. Regards
What is an int in c#?
Explain about Error handling and how this is done
What is exe file in c#?
what is the difference between a struct and a class in c#?
Why delegates are safe in c#?
Why are dynamic link library used over static one?
What is a protected class c#?
What are constants in c#?
What is the difference between protected and internal in c#?
What is an arraylist in c#?
Can a class or a struct have multiple constructors?