0

In the C# specification for Interface (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/interfaces#interface-implementations), there is an example regarding a class implements multiple interfaces:

interface IControl
{
    void Paint();
}

interface ITextBox: IControl
{
    void SetText(string text);
}

interface IListBox: IControl
{
    void SetItems(string[] items);
}

class ComboBox: IControl, ITextBox, IListBox
{
    void IControl.Paint() {...}
    void ITextBox.SetText(string text) {...}
    void IListBox.SetItems(string[] items) {...}
}

My question is not about Implicit and Explicit implementation, I was wondering why people wants to include the base interface(IControl in this example) in the class definition? Isn't following definition good enough?

class ComboBox: /*IControl,*/ ITextBox, IListBox
    {
        void Paint() {...}
        void ITextBox.SetText(string text) {...}
        void IListBox.SetItems(string[] items) {...}
    }

Another example is in the List definition, its base interface list includes the base interfaces of IList<T> and ILit, why?

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

interface IList : ICollection, IEnumerableclass 

List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable 
Bochen Lin
  • 413
  • 4
  • 12
  • The short answer is no. ITextBox implements IControl, and IListBox implements IControl, so you are implementing the IControl implicitly by implementing either one of the ITExtBox or IListBox interfaces. – CodexNZ Aug 15 '17 at 23:28
  • If there is no different, why libraries does that?(I added an example of definition of List in the library). – Bochen Lin Aug 16 '17 at 00:26
  • If you look at MSDN docs for the two list iterface definitions: https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/5y536ey6(v=vs.110).aspx for IList and IList respectively you will see that they are not the same interface, hence the need to implement both interfaces. – CodexNZ Aug 16 '17 at 00:47
  • No, I am not saying why List implements IList and IList, I am saying why List repeat "ICollection" in its base interface list together with IList which also inherits from ICollection? – Bochen Lin Aug 16 '17 at 03:00
  • As itsme86 has stated, this is a duplicate, for more information go to the duplicate question. https://stackoverflow.com/a/598727/2026877 – CodexNZ Aug 16 '17 at 03:06

0 Answers0