سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top