IList List in collection base class?

1.1k views Asked by At

question is Regarding the collection base class that implements a weird property:

protected IList List { get; }

1) What is Ilist List in collection base class?? ...

2) It is not even initialized ... how can it be accessed then ...

3) When to use this list?

3

There are 3 answers

1
Peter On BEST ANSWER

1) IList is a interface, any class that is a implemetation of a IList can be returned by the List property of your class. In other words, you only know it can do the interface specification of IList.

2) You can't, you have to initialize your class before you can get the property List from the class. The IList is initialized as a class which implements the IList interface.

3) That depends of the specification of the class. I would guess it returns the items in the current list as a IList.

5
Darin Dimitrov On

This code will not even compile. Automatically implemented properties must have both get and set accessors.

It will compile if it is inside an abstract base class and marked with the abstract keyword:

protected abstract IList List { get; }

Then it is up to the derived classes to implement the getter of this property.

1
Kirin Yao On

Actually, inside CollectionBase class, the code is as follow:

protected IList List
{
    get
    {
        return this;
    }
}

So, it can be compiled with no error.

When derived classes or CollectionBase itself call List property, it will return the instance of the list(this). If you're designing a subclass of CollectionBase, you can use this property wherever you want to get the instance of the type as IList.