Re: Novice question about inherited constructors

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Oleg Subachev wrote:
I am moving from Delphi to C# and hve encountered the problem:

<snip>

When I try to compile I get the following errors:

No overload for method 'class1' takes '0' arguments

This is because the compiler is creating a default constructor for
class2, which tries to call a parameterless constructor in class1 - but
there isn't one.

No overload for method 'class2' takes '1' arguments

This is because there isn't a constructor in class2 taking a string,
because constructors aren't inherited.

Why inherited constructors are not used (as in Delphi) ?

If constructors were inherited, then *all* classes would have to have a
parameterless constructor, as System.Object does. I don't know how
Delphi gets around it, but personally I'm glad constructors aren't
inherited - it's good to be able to mandate that any instance of a
certain class *must* go through one of the constructors I've specified,
even if extra constructors have been added to the base class.

I view instance creation as somewhat different to instance use - things
which can be treated the same way after they've been created often have
very different creation requirements.

(Having said that, I *would* like some way of specifying what
constructors *must* be present in any type implementing a specific
interface, and possibly likewise for static methods. Extra syntax would
be necessary in order to call those constructors/static methods,
however - it does all get a bit complicated.)

Jon

.



Relevant Pages

  • Re: Novice question about inherited constructors
    ... That is just part of the design of .NET that constructors are not inherited. ... If you have a non-default constructor in a base class, ... public class1(string aS) ... No overload for method 'class1' takes '0' arguments ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Novice question about inherited constructors
    ... because constructors aren't inherited. ... even if extra constructors have been added to the base class. ... I view instance creation as somewhat different to instance use - things ...
    (microsoft.public.dotnet.languages.csharp)