Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 array class in c#?

805


What kind of the information stored inside the assembly?

951


Is a valid int value?

887


How do you specify a custom attribute for the entire assembly (rather than for a class)?

916


Can main method be final?

869


How does bubble sort work?

860


What is the purpose of namespace in c#?

824


How Global.asax is used ?

1014


What are reflections in c#?

857


What Happens In Memory When You Box And Unbox A Value-type?

829


What is the difference between method overriding and method overloading?

880


What is a satellite assembly in c#?

956


What does question mark mean in c#?

947


Does unity use c++ or c#?

842


What do you mean by streamreader/streamwriter class in c#?

908