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

C#.Netvb.netOop

C# Problem Overview


> 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.

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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?

Solution 5 - C#

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.

Solution 6 - C#

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

Solution 7 - C#

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?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPondidumView Question on Stackoverflow
Solution 1 - C#cletusView Answer on Stackoverflow
Solution 2 - C#Jon SkeetView Answer on Stackoverflow
Solution 3 - C#TonyView Answer on Stackoverflow
Solution 4 - C#M.TurriniView Answer on Stackoverflow
Solution 5 - C#Wim CoenenView Answer on Stackoverflow
Solution 6 - C#Mork0075View Answer on Stackoverflow
Solution 7 - C#Damien_The_UnbelieverView Answer on Stackoverflow