Why do we implement interfaces recursively?

I understand that any collection (here I'm talking about regular non-common ones) should implement ICollection, IEnumerable and IList for a regular collection of objects or IDictionary in the case of dictionaries.

[However, the question I ask does not apply to collections]

IList derived from ICollection and IEnumerable

ICollection inferred from IEnumerable

Is it not enough to build (e.g. ArrayList) an IList?

The Object Explorer shows that collection classes (such as ArrayList) implement IList, ICollection, and IEnumerator.

I understand that even if we specify all three collections, .Net will only accept definitions once.

But my question is:

  • Is there any good practice or recommendation that will help us specify all three interfaces for a collection class (or any class like this)?

  • Or is it just an object explorer property that displays it as 3 separate implementations? [Just checked and found that this is not a property of the object browser. The Object Browser simply displays the interfaces specified in the class definition]

+6
collections
source share
5 answers

I believe that this should just be clear to the developers or help provide an interface structure. I mean, imagine that you have an interface for data structure classes and you are implementing IDataObject. IDataObject then implements ISecurable and ILoggable. The regular classes you create can simply implement IDataObject, but what if the creator of IDataObject later changes the implementation and strips ILoggable? This can change the functionality of your code. Thus, to prevent this, when you create your class that inherits from IDataObject, you can directly declare that you want to implement ISecurable and ILoggable too, to be safe.

I don’t know exactly why they did this with IList, but these two reasons are my best guess as to why.

+1
source share

I believe this is just an object browser that displays it that way. I just tried this:

using System.Collections; using System.Collections.Generic; public class Foo : IEnumerable<int>, IEnumerable { public IEnumerator<int> GetEnumerator() { return null; } IEnumerator IEnumerable.GetEnumerator() { return null; } } public class Bar : IEnumerable<int> { public IEnumerator<int> GetEnumerator() { return null; } IEnumerator IEnumerable.GetEnumerator() { return null; } } 

Loading this into the object browser showed both interfaces for both classes.

Please note that sometimes there can be a point for reusing an interface if you inherit from another implementation - this allows you to override the interface if it was previously implemented explicitly or in a non-virtual way. I do not believe this case, but worth mentioning.

In general, you definitely do not need to specify all interfaces, and I usually did not.

+5
source share

Is it not enough to build (e.g. ArrayList) an IList?

It's enough. Class declaration:

 public MyCollectionClass : IList { } 

This means that your MyCollectionClass implements IList, ICollection, and IEnumerable.

The Object Explorer shows that collection classes (such as ArrayList) implement IList, ICollection, and IEnumerator.

This is either a part of the object browser, or the base class simply implemented the collection classes, specifying all the interfaces. However, there really is no reason for this.

+2
source share

Now, I think you are asking, considering

 interface IA {}; interface IB : IA {}; interface IC : IB {}; 

What's the difference between:

 class MyClass : IC {}; 

and

 class MyClass : IA, IB, IC {}; 

And the answer: absolutely nothing . The second version simplifies its development for other programmers, but will either generate identical code.

+2
source share

To access an object through an interface, the class definition for the object must explicitly determine that it implements the interface ...

For example, I might have the following:

 interface IAnimal { public void Yelp(); } 

and the following class:

 class Dog { public void Yelp() { // do yelping } } 

Now the dog really squeals; however, since it does not declare that it implements IAnimal, I cannot do the following:

 IAnimal poodle = new Dog(); poodle.Yelp(); 

To fix this, the definition for Dog should be changed to:

 class Dog : IAnimal { public void Yelp() { // do yelping } } 
+1
source share

All Articles