Why are we not allowed to specify a constructor in an interface?


Question

Possible Duplicate:
Interface defining a constructor signature?

I know that you cannot specify a constructor in an interface in .Net, but why can we not?

It would be really useful for my current project to be able to specify that an 'engine' must be passed in with the constructor, but as I cant, I have to suffice with an XML comment on the class.

1
51
5/23/2017 12:18:14 PM

Accepted Answer

Because an interface describes behaviour. Constructors aren't behaviour. How an object is built is an implementation detail.

88
3/27/2009 11:57:10 AM

How would you call the constructor? When you use interfaces, you normally pass an instance of the interface around (or rather, a reference). Also bear in mind that if one class implements an interface, a derived class inherits that interface, but may not have the same set of constructors.

Now, I can see the use of what I call static interfaces for specifying constructors and other essentially static members for use in generic methods. See my blog post on the idea for more information.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon