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


Please Help Members By Posting Answers For Below Questions

What is multithreading with .net?

787


Why do we use struct in c#?

752


Is javascript harder than c#?

718


What is bitwise operator in c#?

703


What is string in c# net?

709


How many types of collections are there in c#?

687


How can an inner class access the members of outer class?

753


Please explain the basic string operations used in c#?

755


Is c# and .net same?

670


What is the difference between func and action delegate?

719


Is stringbuilder better than string?

728


Define an array?

717


What is check/uncheck?

839


What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)?

746


What is dependency injection in simple words?

723