Hi
C# constructors cannot be inherited. Why ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 14, 2012, 11:09 PM EDT
Constructors are not inherited. In other words, just because a base class has a constructor taking a certain list of parameters doesn't mean that a derived class has a constructor taking that list of parameters. (It can, by providing one itself, but it doesn't inherit it from the base class.) To demonstrate this, here's an example which doesn't compile:
public class MyBaseClass { public MyBaseClass (int x) { } } public class MyDerivedClass : MyBaseClass { // This constructor itself is okay - it invokes an // appropriate base class constructor public MyDerivedClass () : base (5) { } public static void Main() { new MyDerivedClass (10); } }Here, we try to invoke a constructor for MyDerivedClass which takes an int parameter. There isn't one, however, as constructors aren't inherited. The MyBaseClass constructor which takes an int parameter can be invoked by a constructor in MyDerivedClass (as is shown by the parameterless MyDerivedClass constructor) but isn't actually inherited. Removing the "10" from the above code would make it compile and run with no problems - the parameterless MyDerivedClass constructor would be invoked, and that would in turn invoke the MyBaseClass constructor taking an int parameter, with 5 as that parameter value.
Please refer the below link
http://www.yoda.arachsys.com/csharp/constructors.html
Thanks
VulpesRank #55 · 29.2K reputationPosted Jul 15, 2012, 6:12 AM EDT