After several very frustrating hours of work I discovered that C# constructors are not inherited.   To be more precise, constructors with parameters are not in inherited.  I had a situation where I had created a class called “tag” that provided 99% of the functionality required.  However, it was used by two other classes and both needed a different constructor.  I spent several hours going through the online help, websites and trying different approaches before I finally came across http://www.yoda.arachsys.com/csharp/constructors.html.  The last few paragraphs are quoted:

Constructors are not inherited

(ECMA) (MS), second paragraph from the bottom.
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.

Some people have said that they would rather constructors were inherited, making the language act as if all derived classes had constructors with all the parameter lists from the constructors from the base class, and just invoking them with the parameters provided. I believe this would be a very bad idea. Take, for instance, the FileInfo class. You must logically provide a filename when constructing a FileInfo instance, as otherwise it won’t know what it’s meant to be providing information on. However, as object has a parameterless constructor, constructors being inherited would then mean that FileInfo had a parameterless constructor. Some have suggested that this could be fixed by allowing you to “override” the parameters you didn’t want invoked as private, but this goes against the idea that you should never be able to override anything to give it more restrictive access, and also means that class developers would have to change their code every time a new constructor was added to a base class.

Source: Constructors in C#

Advertisements