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 ac callback?

680


What is method and function in c#?

725


What is difference between array and collection in c#?

661


What happens if a static constructor throws an exception?

712


Is dictionary reference type c#?

651


How does bitwise xor work?

656


List the two important objects of ado.net and also list the namespaces that are commonly used in ado.net to aid in connection to a database.

705


How can I develop an application that automatically updates itself from the web?

679


Can we extend sealed class in c#?

691


Why it's said that writing into .NET Application Configuration Files is a Bad Idea?

754


Can abstract class have private constructor c#?

723


What's the difference between class and object?

705


What is the implicit name of the parameter that gets passed into the class set method?

675


what is IDisposal interface

918


These questions were asked me in a technical interview: •If we deploy an application on multiple server (like database server, web server) then, each request should be redirected to proper server, then how you will handle it in your code? •How security pinholes will be handled in an application? •What things should be considered while writing a web application? •How will you do load/performance testing of web application? Which framework you will use for it? •How will you implement a cache for results which require a DB access? Please let me know how to write an web application considering all these points. I am not so much aware of architechural design of web application. Your guidelines will be helpful.

2752