tags:

views:

920

answers:

7

Well the title says it all really.

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.

+2  A: 

Because you cant instantiate an interface, so a constructur doenst make sense.

Mork0075
+11  A: 

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.

Jon Skeet
+1 I think this would be a great addition to .NET and I've upvoted the associated Connect feedback item: https://connect.microsoft.com/VisualStudio/feedback/details/412848/allow-structural-type-constraints-for-generic-type-parameters
Evgeny
+25  A: 

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

cletus
I think that's putting attaching too much meaning to an implementation detail: a constructor in .NET is really just a method that gets called immediately after the object is created (allocated in memory). There is no reason it can't be defined as behaviour as well. Of course, implementations are free to declare other constructors as well, just as they can overload a method that implements an interface.Constructors in interfaces would be a rarely-used feature, but not a useless one.
Evgeny
+4  A: 

No you can not have constructors on interfaces for the reasons that have been posted. However you can on abstract classes. Lets say for example you have this base class.

public abstract class ClassOne
{
    protected int _x;
    protected string _s;

    public ClassOne(int x, string s)
    {
        _x = x;
        _s = s;
    }        
}

Notice there is no constructors that takes no argument (default constructor) which means any class that inherits from ClassOne must call the constructor that has 2 arguments.

So this is not valid and will not compile.

public class ClassTwo : ClassOne
{
    public ClassTwo() 
    { }
}

However this is valid and will compile.

public class ClassTwo : ClassOne
{
    public ClassTwo(int x, string s) : base(x, s)
    {  }
}

I would like to point out here that in C# you can only inherit from one base class. Meaning that this may not be the correct solution for particular situation but is something to think about.

Tony.

Tony
+4  A: 

Among all the other reasons already posted, keep also in mind the a class can easily implement several interfaces; which constructor should be used then?

Turro
There's a perfectly good syntax for implementing interface methods with the same signature that could be used for the constructors.
Simon
I'm not sure I got your point (maybe I didn't): I meant that class A can implement interfaces IAlpha with a constructor with no arguments, IBeta with a constructor with one arguments and IGamma with a constructor with two arguments: which constructor should class A implement?
Turro
Ah, this is the only answer that really makes sense to me. All the arguments that you can't instantiate an interface leave me asking "So what?" The point of an interface is to declare that such-and-such a set of functions must be implemented in any class that presumes to implement the interface. It sounds reasonable to say that such a class should also have to implement a constructor taking this particular set of arguments. But the clashing constructors argument, now that presents a problem.
Jay
Totally agree with Jay. Although I believe its not an absurd idea to enforce constructor signature through interface, a lot more thought needs to be put in.
Shravan
+2  A: 

Other answers have already pointed out why it doesn't make sense to have a constructor declaration on an interface. But from your question, I'm guessing that you are probably looking for the abstract factory pattern.

To give an example based on your question: you say that you would like to somehow declare that an 'engine' must be passed to the constructor. You can do this by declaring a separate interface for a construction service like this:

public interface IGadgetFactory
{
   IGadget CreateGadget(Engine engine);
}

Any code which must create IGadget instances can then use an IGadgetFactory instance instead of calling any constructors directly.

Wim Coenen
A: 

Besides the other explanations given here, you'd have to invent a new syntax for calling them anyway, since if you have two or more implementations in scope at the line:

Dim x as new IDoStuff()

Whice implementation gets called?

Damien_The_Unbeliever