16

I would like to only implement certain interfaces within other interfaces, I don't want them to be able to be inherited directly by a class.

Thanks in advance!

thecoop
  • 45,220
  • 19
  • 132
  • 189
  • 4
    I've re-read the question and I still think you need an example – mythz Feb 17 '10 at 18:42
  • 1
    Reading the question again, I think you're asking for some construct to prevent **direct** implementation of an interface in a class while not preventing other interfaces to inherit from it. – Mehrdad Afshari Feb 17 '10 at 18:44
  • Example: I have an interface called IAnimation, I wish to only implement this interface through an interface INonAnimated or IAnimated. That way both child interfaces hold the same base rules. – Andrew Camilleri 'Kukks' Feb 17 '10 at 18:47
  • 1
    Perhaps having the IAnimation interface as an Abstract will allow you to derive two more interfaces from IAnimation as you cannot create an object of it, only derived from it. – Jamie Keeling Feb 17 '10 at 19:09
  • I wanted to do this to "conditionally require" one property/method or another, but not neither; all of this while maintaining some shared properties/methods in the parent "abstract interface". – Ben Mosher Feb 07 '13 at 17:23
  • You should mark IAnimation as internal, that way other assemblies can only access your public interfaces, INonAnimated and IAnimated – Ricardo Rodrigues Jul 17 '13 at 11:31

3 Answers3

27

You can't do this in C# - any class can implement any interface it has access to.

Why would you want to do this? Bear in mind that by declaring an interface inheritance:

public interface InterfaceA {}
public interface InterfaceB : InterfaceA {}

You're specifying that anything implementing InterfaceB also has to implement InterfaceA, so you'll get classes implementing InterfaceA anyway.

thecoop
  • 45,220
  • 19
  • 132
  • 189
  • public interface InterfaceA {} public interface InterfaceB : InterfaceA {} public interface InterfaceC : InterfaceA {} InterfaceA would hold the common [i]rules[/i] that I would be using in InterfaceB and InterfaceC – Andrew Camilleri 'Kukks' Feb 17 '10 at 18:53
1

First of all, it doesn't make sense to say "implement within other interfaces" because interfaces can't implement anything.

I can see two flawed ways of doing this, sort of.

  1. Make Animated and NonAnimated abstract classes that implement IAnimation. The concrete class below them can still forcibly override your IAnimation methods with the new operator:

    class SomeAnim : Animated
    {
        public new void Foo() { }
    }
    
  2. Use mixins. Keep IAnimated and INonAnimated as interfaces, but don't put any methods in your interface. Instead define extension methods like this:

    static class Ext
    {
        public static void Foo(this IAnim anim)
        {
            if (anim is IAnimated) // do something
            else if (anim is INonAnimated) // do something else
        }
    }
    

again, a bit of a hack. But what you're trying to do indicates design flaws anyway.

Tesserex
  • 17,166
  • 5
  • 66
  • 106
  • re code formatting, see here: http://meta.stackexchange.com/questions/19624/bug-in-markdown-formatter/19799#19799 You need to indent your code by 8 spaces rather than 4 – thecoop Feb 17 '10 at 19:08
-1

The best I can suggest is to put them -- both the top level and the descended interfaces, in a separate assembly, with the base-level interfaces declared as internal, and the interfaces which extend those interfaces as public.

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • Is it then possible to actually implement the top level interfaces, if super-interfaces aren't accessible to the implementing class? – thecoop Feb 17 '10 at 18:52
  • I get this: `Inconsistent accessibility: base interface 'IBaseInterface' is less accessible than interface 'IDescendantOfBaseInterface'`. C# 4.0. – Ben Mosher Feb 07 '13 at 17:21