0

Please, note that I don't ask how to do something. This question is about design decision by language authors.

Let's say you have a base class with constructor and a method:

public class BaseClass
{
    public BaseClass(string argument)
    {
    }

    public void SomeMethod()
    {
    }
}

And you also have derived class that is empty (or, in reality, has other unrelated stuff):

public class DerivedClass : BaseClass
{
    public DerivedClass() // Some other constructor not used in the example, just so it will compile
}

So, when you will try to call the method on an instance of the derived class, it will work, but if you try to call the constructor, it will result in compile error:

var d = new DerivedClass("argument"); // error CS1729: The type `DerivedClass' does not contain a constructor that takes `1' arguments
d.SomeMethod(); // Works OK

What is the reason behind this language design decision?

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • 5
    Your class definitions are invalid and will raise error CS1729: The type BaseClass does not contain a constructor that takes 0 arguments. –  Jul 15 '15 at 15:40
  • 2
    DerivedClass inherits BaseClass but it has its own constructor. – M.kazem Akhgary Jul 15 '15 at 15:41
  • 3
    Obviously to annoy you. Or maybe they wrote that part of the spec on Monday. Or maybe because tuna. –  Jul 15 '15 at 15:41
  • @hvd, edited the question – Max Yankov Jul 15 '15 at 15:44
  • 3
    Can the person who tagged this question as related to networking or server related please, read the question? – Max Yankov Jul 15 '15 at 15:45
  • What if you want `public DerivedClass(string instanceName)`? Probably much easier and more clear to just use a ctor, `public DerivedClass(string arg) : base(arg) {}` – Kcvin Jul 15 '15 at 15:45
  • 1
    Restored the disclaimer that was removed by @Jashaszun. Judging by the first (already deleted) answer, the disclaimer is *very* necessary. – Max Yankov Jul 15 '15 at 15:47
  • 1
    You code still won't compile as `DerivedClass` doesn't have a valid constructor. Voting to close as it's unclear what you are asking. – David Arno Jul 15 '15 at 15:47
  • @MaxYankov But when you add a constructor to `DerivedClass`, then it's already obvious that the base class constructors won't and shouldn't somehow be inherited, isn't it? I may have been mistaken, but I thought your question was specifically about derived classes that don't explicitly declare any constructor. –  Jul 15 '15 at 15:47
  • 1
    possible duplicate of [Why are constructors not inherited?](http://stackoverflow.com/questions/426484/why-are-constructors-not-inherited) – Max Yankov Jul 15 '15 at 15:49
  • @DavidArno what is unclear about the question? Yes, it may not compile, the purpose of the code is just to illustrate the question. – Max Yankov Jul 15 '15 at 15:50
  • @MaxYankov, the error won't occur where you are showing it; it will occur when compiling `DerivedClass`. You question only makes sense now I've read answers from people who decoded it. – David Arno Jul 15 '15 at 15:52

1 Answers1

3

This is because constructors are not inherited.

For more info, see this post for an explanation as to why this is: Why are constructors not inherited?

Community
  • 1
  • 1
Nitesh Patel
  • 631
  • 3
  • 10