Why ArrayList implement IList, ICollection, IEnumerable?

前端 未结 4 559
栀梦
栀梦 2020-12-20 11:59

ArrayList declares that it implements the IList, ICollection, and IEnumeralbe interfaces.

Why not only implement

4条回答
  •  别那么骄傲
    2020-12-20 12:28

    I am not so sure that ArrayList has separate implementations of the interfaces. Consider the following code:

    public interface IBase
    {
        int SomeInt { get; set; }
    }
    
    public interface ISub : IBase
    {
        int SomeOther { get; set; }
    }
    
    public class MyClass : ISub
    {
        public int SomeOther { get; set; }
        public int SomeInt { get; set; }
    }
    

    The MyClass type implements only the ISub interface directly. However, if you compile the code into an assembly, and then add that assembly as reference in another project, open the Object Browser and examine the base types for MyClass, it will feature something like this:

    Base Types
     |- ISub
     |    |- IBase
     |- IBase
     |- Object
    

提交回复
热议问题