I have always had this question in my mind. An interface forces the implementer class to have specific methods, or properties, or events. But why couldn't it force the implementer class to have a specific constructor?
For example:
interface IX
{
ctor(int val);
string Foo();
}
..and this means that every class which implements IX
should have a constructor with an int
as a parameter.
Usage of this is not straight-forward, this only guarantees that if a class has implemented the IX
interface, nobody in your development team has forgotten to put that constructor in his class. Consider the situation that these classes are instantiated by reflection or Activator
.