C# constructors annoy me!

  • Thread starter Thread starter Peter Morris [Droopy eyes software]
  • Start date Start date
Jon said:
In that case, what would you expect to happen if classes inherited
constructors and someone used the parameterless contructor inherited
(directly or indirectly) from System.Object?

One could propose a variance of default-construction that could be
claimed to be akin to "constructor-inheritance" and which makes a bit
more sense than the parameterless-default-constructor definition.

Instead of a parameterless constructor, a class will have
default-constructors corresponding to it's super-class' constructors,
all implemented by simply invoking base(...) with the arguments.

Notice that for classes with only a parameterless constructor this
yields the same semantics as now.

If *any* constructor is declared in a class, *no* default constructors
are generated, as it is now.

Also, defaulting to constructing the super-class using it's
parameterless constructor is somewhat error-prone. One could argue that
required explicit declaration of parent-construction would be "in the
spirit" of C#.

But all that's too late now, we're stuck with a default parameterless
constructor in C# -- but so are most other languages so it's not much of
a competitive parameter.
 
In that case, what would you expect to happen if classes inherited
constructors and someone used the parameterless contructor inherited
(directly or indirectly) from System.Object?

That's simple. In the base class I would implement a parameterless
constructor and mark it as obsolete.
 
Peter Morris said:
That's simple. In the base class I would implement a parameterless
constructor and mark it as obsolete.

Euch - I really don't like that idea. For one thing, it means that you
have to do that for every overload you *don't* want to support for all
your base classes - and if an extra constructor is added to the base
class in a new version, you may not even notice. Do you really want to
have to check for new constructors for all your base classes all the
time?

I think it's better to say what constructors you explicitly *do* want
to support, personally.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top