Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we implement interfaces recursively?

I understand that any Collection (here I am talking about regular non-generic) should have implemented ICollection, IEnumerable and IList incase of regular object collection or IDictionary in case of Dictionaries.

[Still, the question I ask is not specific to collections]

IList is derived from ICollection and IEnumerable

ICollection is derived from IEnumerable

Is it not just enough to make a collection (E.g. ArrayList) implement IList?

In the Object browser it is displaying that collection classes (E.g. ArrayList) are implementing IList, ICollection, and IEnumerator.

I understand that even if we specify all three Collections, .Net is going to accept the definitions only once.

But my question is,

  1. Is there any best practice or recommendation that guides us to specify all three interfaces for the collection class (Or any class similar to this)?

  2. Or is it just the propery of Object Browser that displays it as 3 separate implementations? [Just checked and found that its not the property of Object browser. Object browser just displays the interfaces that are specified in the class definition]

like image 939
SaravananArumugam Avatar asked Aug 16 '10 19:08

SaravananArumugam


4 Answers

I believe it's just the object browser that displays it that way. I've 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 on both classes.

Note that sometimes there can be a point to redeclaring an interface if you're inheriting from another implementation - it allows you to reimplement the interface if it's previously been implemented explicitly or in a non-virtual way. I don't believe that's the case here, but it's worth mentioning.

In general though, you definitely don't have to specify all the interfaces, and I wouldn't generally do so.

like image 155
Jon Skeet Avatar answered Oct 24 '22 23:10

Jon Skeet


Is it not just enough to make a collection (E.g. ArrayList) implement IList?

It is enough. Declaring a class:

public MyCollectionClass : IList {
}

That will mean your MyCollectionClass implements IList, ICollection, and IEnumerable.

In the Object browser it is displaying that collection classes (E.g. ArrayList) are implementing IList, ICollection, and IEnumerator.

This is either a detail of the Object Browser, or else the base class has simply implemented the collection classes by specifying all interfaces. There really isn't any driving reason to do so, however.

like image 30
quentin-starin Avatar answered Oct 24 '22 23:10

quentin-starin


Now, I think you are asking, given

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 is, absolutely nothing. The second version makes it design a bit clearer to other programmers, but either will generate identical code.

like image 32
James Curran Avatar answered Oct 24 '22 21:10

James Curran


I believe this is just either to be clear to developers, or to help enforce the interface structure. What I mean is, imagine you have an interface for data structure classes, and you implement IDataObject. IDataObject then implements ISecurable, and ILoggable. Regular classes you create could just implement IDataObject, but what if the creator of IDataObject changes the implementation later and drops ILoggable? This may change the functionality of your code. So, to prevent this, when you create your class that inherits from IDataObject, you can explicitly say that you want to also implement ISecurable and ILoggable, just to be safe.

I don’t know for sure why they did that with IList, but those two reasons are my best guess on why.

like image 37
Robert Seder Avatar answered Oct 24 '22 23:10

Robert Seder