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
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 |
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 |
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 |
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 |
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 |
can you allow a class to be inherited, but prevent the method from being over-ridden?
Distinguish between a class and struct?
What are async and await?
What is icollection in c#?
What does private void mean in c#?
Can firstordefault return null?
Explain the difference between “constant” and “read-only” variables used in c#?
Assignment # 1 Print the pattern given below using single printf statement XX XX XX XX XX XX X X X X XX XX XX XX XX XX Assignment # 2 Print the pattern given below using single printf statement (please do NOT use the string function below) 0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321
What is a jagged array?
What?s the difference between the Debug class and Trace class?
Distinguish between continue and break statement?
can you declare an override method to be static if the original method is not static?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)