Ben Gribaudo Ben Gribaudo - 16 days ago 6
C# Question

Class Keyword - Explicitly Indicating Inherited Interfaces?

SqlDataReader's class statement includes IDataReader, IDataRecord and IDisposable even though these are all implemented by its base class, DbDataReader:

public class SqlDataReader : DbDataReader,
IDataReader, IDisposable, IDataRecord {...}

public abstract class DbDataReader : MarshalByRefObject,
IDataReader, IDisposable, IDataRecord, IEnumerable {...}


In this case, is there some technical advantage to indicating that the derived class implements interfaces which its base class already indicates that it implements? (I can't think of one. Wondering if this is a legacy relic, a typo or something done for documentation purposes.)

Answer

This could be done in order to add or override explicit interface implementations in the derived class. For example,

interface IFoo
{
    string P {get;}
}

class Base: IFoo
{
    string IFoo.P 
    {
        get { return "Base"; }
    }
}

class Derived: Base, IFoo
{
    string IFoo.P 
    {
        get { return "Derived"; }
    }
}

If Derived doesn't implement IFoo directly, it cannot define an explicit implementation of IFoo.P, so it cannot override the implementation in the base class.