C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?

Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there's no implementation in it.

Showing Answers 1 - 6 of 6 Answers

PavanKumar

  • Nov 18th, 2006
 

Hi,In C#.Net we can have multiple constructors.Before going any further I think you need to know a liitle bit about the constructors. Basically, we have few types of constructors which are called on the basiis of the pparameters passed or depending on the access modifiers. Generally, we come to see a default constructor in our code. which is public. whose instances can be created. If you requirement is such that you dont wanna create an instance of your class then make the constructor to private. If your requirement is such that irrespective of the number of instances created you want only One Constructor to be called then make the construuctor to be staic.Now coming to your case you need to have two constructors 1) Parameteriized Constructor which takes string as input 2) And default Constructor what we come to see in our real life. The constructor is called at the runtime based on the parmeters. If at all you are sending any parameters as: ClassExample CE=new ClassExample(); // This calls the default Constructor string s; s="Pavan"; ClassExample CE=new ClassExample(s); // This calls the Parameterized Constructor Get Back To Me If You have any Further queries Thanks & Regards PavanKumar.S Pavan

  Was this answer useful?  Yes

sgaron

  • Oct 26th, 2017
 

One constructor is enough. In every constructor program we can't create number of constructor. We can create the number of classes in constructor. so here we can give parameters in method only we can declare the parameters.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions