6

I am a bit unclear on the syntax of inheritance for interfaces in C#.

For example:

public interface IFoo
{
}
public interface IBar : IFoo
{
}

What is the difference between this:

public interface IQux : IBar
{
}

and this:

public interface IQux : IBar, IFoo
{
}

Or, for a real world example, Why is ICollection<T> declared like this:

public interface ICollection<T> : IEnumerable<T>, IEnumerable

instead of this:

public interface ICollection<T> : IEnumerable<T>

given that IEnumerable<T> already inherits from IEnumerable?

Mr Anderson
  • 2,200
  • 13
  • 23
  • 1
    It's not necessary to explicitly list the base interfaces as implemented, but it's good for clarity. IOW, there is no functional difference. – Blorgbeard May 10 '16 at 22:42
  • agreed with @Blorgbeard, there is no difference, just good for clarity – Brian Ogden May 10 '16 at 22:43
  • 2
    @xandercoded you can't delete and repost downvoted answers and then complain about gamification. You're clearly playing the game yourself. – Blorgbeard May 10 '16 at 22:56

3 Answers3

5

Eric Lippert explains it very well in this article:

https://blogs.msdn.microsoft.com/ericlippert/2011/04/04/so-many-interfaces

If IBar inherits from IFoo, then, from the compiler's perspective, there is no difference between:

public interface IQux : IBar
{
}

and this:

public interface IQux : IBar, IFoo
{
}

You can choose to state that IQux incorporates IFoo if you think it makes the code more readable. Or you can choose not to.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
2

Generics didn't exist from the start -- look at the C# 1.0 docs and you won't see IEnumerable<T>.

Regarding the first question: there is no difference (not even with explicit interface implementations as far as I can tell).

Conside this:

public interface IFoo
{
    void M();
}

public interface IBar : IFoo { }
public interface IQux : IBar, IFoo { }
public interface IQux2 : IBar { }

// Both work:
// class X : IQux
class X : IQux2
{
    void IFoo.M() { }
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • ^^ What he said. Although I think it sometimes comes down to how you want to USE your interfaces. You might want to enforce a class that implements IBar to also implement IFoo. Or maybe there is something about IFoo that doesn't make sense for IBar. I think it really comes down to what *YOU* want out of your interfaces and *HOW* you plan to use them or how they relate to each other. – Mr. Young May 10 '16 at 22:46
1

Relevant quote from the C# Specification (version 5), section 13.4:

A class or struct that directly implements an interface also directly implements all of the interface’s base interfaces implicitly. This is true even if the class or struct doesn’t explicitly list all base interfaces in the base class list.

So, there is no need to explicitly list base interfaces. I assume it is only done for clarity for the developer.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • @xandercoded In what way is it good for polymorphism? – DavidG May 10 '16 at 22:48
  • @xandercoded you should undelete one of your answers and edit to explain what you mean. The comments section of an unrelated answer is not the place. – Blorgbeard May 10 '16 at 22:52
  • And you still seem to be missing the point: it's not necessary to *declare* that `ICollection` inherits from `IEnumerable`, because it inherits from `IEnumerable`, which inherits from `IEnumerable` anyway. So whether you declare it or not, it inherits from `IEnumerable`. The question was not about why it should do that, it was about the syntax required. – Blorgbeard May 10 '16 at 22:55
  • @xandercoded Your answers were downvoted because people found them not useful. Your comment here that it's "good for polymorphism" seemed to show you didn't even read this answer too! – DavidG May 10 '16 at 22:56
  • @xandercoded Yes, missing the point. *Not* "declaring it like this" - i.e. explicitly listing all base interfaces - would make no difference to the compiled code. – Blorgbeard May 10 '16 at 22:59
  • 3
    @xandercoded - I didn't downvote you. But, you made some generic statements about interfaces, without answering this specific question. The specific question was not "Why does `ICollection` implement `IEnumerable`". The specific question was "Why is `ICollection` declared as explicitly implementing `IEnumerable`, when it already does so implicitly." – Andrew Shepherd May 10 '16 at 23:00