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?

Answers were Sorted based on User's Feedback



Can we have private constructor in our class file. When we are trying to create instance for the cl..

Answer / 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

Can we have private constructor in our class file. When we are trying to create instance for the cl..

Answer / balamurugan

we can create private constructor.it wont throw any
execption..but we cant create object for that class

Is This Answer Correct ?    3 Yes 3 No

Can we have private constructor in our class file. When we are trying to create instance for the cl..

Answer / vijay rana

we can declare objects in csharp,
but we can not create any instance of this class ,
if we do so it will throw an exception;

Is This Answer Correct ?    4 Yes 4 No

Can we have private constructor in our class file. When we are trying to create instance for the cl..

Answer / saiish bhat

ya,we have private constructor.but it wont throw an exception.

but u cant create an instance of p.c.

it is not possible to inherit from that class also.

ex:
private class a()
{

}

class b : a
{ }

Is This Answer Correct ?    1 Yes 2 No

Can we have private constructor in our class file. When we are trying to create instance for the cl..

Answer / ranjeet saini

it does not throw an exception
bcoz we can create private constructor if we have static member in class
in this we can create a reference object with the help of static method.
class A
{
private A()
{
C.w.l(" Hi");
}
public static display()
{
return new A();
}
S V M ( S[] args)
{
A a=a.display()
}

}

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More C Sharp Interview Questions

What is expression tree in c#?

0 Answers  


What is the Use Of Interfaces? For example I have a interface as shown below? Interface IMyInterface { public void MyMethod(); } class MyClass : IMyInterface { public void Mymethod() { Some Code } } class Program { static void Main(string[] args) { MyClass obj = new MyClass(); obj.MyMethod(); } } Here What is My Question is? If i remove Interface and run this code, it will executed then what is the Use of the interface? Can any one give me the solution for this Problem? Thanks in Advance!

1 Answers   HCL,


What is executenonquery c#?

0 Answers  


what is the difference between interface and abstraction?

5 Answers   Choice Solutions, IBM,


What are the examples of data types?

0 Answers  






What is selector c#?

0 Answers  


What is the use of ienumerable in c#?

0 Answers  


Why do we need generics in c#?

0 Answers  


How to put assembly in gac?

0 Answers  


How many catch blocks can be there for a single try block?

5 Answers   Clover, HCL,


Why delegates are safe in c#?

0 Answers  


What is Interface and Abstraction (in real time scenario)

4 Answers   HCL,


Categories