Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Constructor in Interface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cristiano de Pádua Milagres Oliveira - 17 Aug 2006 21:46 GMT
Constructor in a Interface is possible?
If yes, what's the C# syntax?
Michael - 17 Aug 2006 21:54 GMT
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?
Marc Gravell - 17 Aug 2006 21:54 GMT
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
Cristiano de Pádua Milagres Oliveira - 17 Aug 2006 22:08 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.