PC Review


Reply
Thread Tools Rate Thread

Can an Interface be implemented only by a particular type of class?

 
 
Emilio
Guest
Posts: n/a
 
      22nd Apr 2009
Good evening to everyone

as the subject says,

can an Interface be implemented only by a particular type of class
(for example by a "Car" class).

Best regards,
Emilio
 
Reply With Quote
 
 
 
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      22nd Apr 2009
On Apr 22, 10:46*am, Emilio <emilio.dedomini...@gmail.com> wrote:
> Good evening to everyone
>
> as the subject says,
>
> can an Interface be implemented only by a particular type of class
> (for example by a "Car" class).
>
> Best regards,
> Emilio


Hi,

No, any class can implement an interface. Of course if the class has
access to the interface. For example, if an interface is declared as
internal no class outside the assembly can implement it but ANY class
in the assembly can.
 
Reply With Quote
 
Emilio
Guest
Posts: n/a
 
      22nd Apr 2009
On 22 Apr, 16:52, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.com> wrote:
> On Apr 22, 10:46*am, Emilio <emilio.dedomini...@gmail.com> wrote:
>
> > Good evening to everyone

>
> > as the subject says,

>
> > can an Interface be implemented only by a particular type of class
> > (for example by a "Car" class).

>
> > Best regards,
> > Emilio

>
> Hi,
>
> No, any class can implement an interface. Of course if the class has
> access to the interface. For example, if an interface is declared as
> internal no class outside the assembly can implement it but ANY class
> in the assembly can.


Thank you so much Ignacio.

Emilio
 
Reply With Quote
 
Pavel Minaev
Guest
Posts: n/a
 
      22nd Apr 2009
On Apr 22, 7:46*am, Emilio <emilio.dedomini...@gmail.com> wrote:
> Good evening to everyone
>
> as the subject says,
>
> can an Interface be implemented only by a particular type of class
> (for example by a "Car" class).


There are some hacks with generics, technically... for example:

interface IFoo<T> where T : Car, IFoo<T> { ... }

Now a class C can implement IFoo<C> only if it's derived from Car.

In practice, this is rather pointless, because you really get a family
of interfaces here, with distinct one for each T - so you cannot use
them for polymorphism. Furthermore, the original idea (interface
that's only implemented by a single class) has the same flaw -
interfaces enable polymorphism, and using them for one class defeats
that purpose, so why would you even want to do that?
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interface is not implemented by this class? Codebehind Jason Microsoft VB .NET 1 28th Jun 2008 01:35 PM
Generic type implementers, class and interface versions not compatible HC Microsoft C# .NET 1 14th Feb 2006 02:09 PM
ldap DirectoryServices.DirectoryEntry System.NotImplementedException: Handling of this ADSVALUE type is not yet implemented (type = 0xb). m96 Microsoft C# .NET 4 22nd Feb 2005 06:11 AM
Type casting object type to user defined class or interface Programmer Microsoft C# .NET 6 17th Jan 2004 08:15 AM
Reflection: CreateInstance(Type) of class that supports known interface - Get actual interface reference to object? Chris Morse Microsoft VB .NET 10 16th Jan 2004 01:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:51 PM.