No. Not possible. Interfaces are never created.
Mike
http://www.seeknsnatch.com
> Constructor in a Interface is possible?
> If yes, what's the C# syntax?
No, it isn't supported directly, as CLR interfaces operate only on
instances that already exist (hence no ctors nor statics) You could,
however, provide a factory method on the interface (or a second
interface), i.e.
public interface ISomethingFactory {
ISomething Create();
}
Note also that with generics (2.0), the presence of a default ctor can
be specified with the new() clause, e.g.
public void SomeMethod<T>(T something) where T : ISomething, new() {}
Marc
Maybe I hadn't explain correctly.
What I want know is if is possible define in the Interface the header of the
Class Constructor that is inheriting..
> Constructor in a Interface is possible?
> If yes, what's the C# syntax?
Marc Gravell - 17 Aug 2006 22:22 GMT
No, it isn't. Concrete types define their ctors along their own needs.
After all, you can only (with the exception of where T : new()) create
the actual object by using the concrete ctor (even if using a factory),
or by using reflection.
So no. Not by interfaces.
Marc
sloan - 17 Aug 2006 23:40 GMT
I think you're saying
"Can I .. in the Interface .. force a concrete class to implement a certain
constuctor?"
The answer is no. I've found myself at times thinking "That would be nice".
I think the small work around is
interface ICheckMeOut
abstract void Initialize(string abc);
then your concrete has to implement that.. and you call that in your
concrete constructors.
not really clean, I know.
> Maybe I hadn't explain correctly.
> What I want know is if is possible define in the Interface the header of the
> Class Constructor that is inheriting..
>
> > Constructor in a Interface is possible?
> > If yes, what's the C# syntax?