Re: No parameterless constructor defined for this object [.NET design flaw?]

  • Thread starter Thread starter Jay B. Harlow [MVP - Outlook]
  • Start date Start date
J

Jay B. Harlow [MVP - Outlook]

Gary,
This is the nature of how constructors work in .NET.

Constructors are never inherited.
If you do not supply a constructor, a default (parameter-less) constructor
will be supplied for you.
If you supply a constructor, then a default constructor will not be created.
If the base class does not have a default constructor, then the derived
class must supply a constructor (C# won't know which base constructor to
call, or what to pass as for parameters).

See:
http://msdn.microsoft.com/library/d...y/en-us/csspec/html/vclrfcsharpspec_10_10.asp
http://msdn.microsoft.com/library/d...en-us/csspec/html/vclrfcsharpspec_10_10_4.asp

In your first example, if .NET automatically gave you a default constructor,
consumers of your class would be able to bypass your defined constructor,
which is not good. You as designer of the class need to specify all the ways
the consumer of the class can create an instance of your class. If one
method is a parameter-less constructor you need to define that.

Yes! its a pain to need to define 10 constructors if the base class has 10
constructors, however IMHO its better than allowing consumers of your class
to create object's by-passing a defined constructor.

Hope this helps
Jay
 
Ok,

Fair enough, I can apperciate it by design. It is just ashame that the
framework handles it so badly, specifically with its intermittency.

Thanks for the answer,

Regards,

Gary Brewer
 
Back
Top