15

I was looking at the declaration of List<T> and saw that it implements IList<T>, ICollection<T> and IEnumerable<T>(among others).

Then I went to look the definition of IList<T> and saw that it implements ICollection<T> and IEnumerable<T>.

What's the point of an interface implementing another interface if they work just as "contracts" and we write no real code at them?

Is this implementation cumulative? If it is, since IList<T> implements ICollection<T> and IEnumerable<T>, List<T> shouldn't implement only IList<T>?

Sorry if my question is confusing, I'm a litte bit puzzled right now.

5
  • 4
    It's for readability. You don't need to go further to see the most important contracts. It also ensures the contracts are not changed significantly if IList<T> ever stops implementing ICollection<T> Commented Jul 24, 2014 at 22:57
  • 2
    actually an interface doesn't implement anything but it can inherit from other interfaces Commented Jul 24, 2014 at 22:59
  • 1
    @zerkms: You should make that an answer! Commented Jul 24, 2014 at 23:00
  • @minitech: it will duplicated in few minutes, it was asked and answered number of times here. Commented Jul 24, 2014 at 23:03
  • 1
    For the question "List<T> shouldn't implement only IList<T>", the reference source referencesource.microsoft.com/#mscorlib/system/collections/… shows the actual implementation. The fact that VS shows IEnumerable, ICollection, etc. is just VS being goofy, the programmer who wrote List<T> didn't explicitly write the implementation statement. Commented Jul 25, 2014 at 3:16

2 Answers 2

20

What's the point of an interface implementing another interface if they work just as "contracts" and we write no real code at them?

Interfaces support inheritance and facilitate polymorphism in the same way that classes do. An IList<T> is also an ICollection<T>, in the same way that a TextBox is a Control.

Is this implementation cumulative? If it is, since IList implements ICollection and IEnumerable, List shouldn't implement only IList?

As others have pointed out, this is mostly for readability and maintainability. Yes, inheritance is cumulative. But, when a class implements multiple interfaces that contain members with the same signature, it only needs to define one member for all implementations. Explicit interface implementation may be used to explicitly define a member for a given implementation.

Sign up to request clarification or add additional context in comments.

Comments

9

At the very least it reduces redundancy.

public interface INamedObject { string Name { get; } }
public interface IValuedObject<T> { T Value { get; } }
public interface INamedValuedObject<T> : INamedObject, IValuedObject<T> { }

Is better design than:

public interface INamedObject { string Name { get; } }
public interface IValuedObject<T> { T Value { get; } }
public interface INamedValuedObject<T> {
   string Name { get; }
   T Value { get; }
}

Also, in the latter case, notice that an INamedValuedObject<T> is now neither an INamedObject nor an IValuedObject<T>.

You can read the former case as "Something that is an INamedValuedObject<T> is something that is an INamedObject, an IValuedObject<T>, and also has the following elements".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.