1

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?

Sana.91
  • 1,999
  • 4
  • 33
  • 52

3 Answers3

3

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.

Kirin Yao
  • 1,606
  • 2
  • 14
  • 21
  • @Dhananjay LOL, he said in *collection base class*, so I examined the source code of `CollectionBase`.:) – Kirin Yao May 25 '12 at 01:21
2

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.

Peter
  • 27,590
  • 8
  • 64
  • 84
  • So its just the property named as **List** ... it does not have to be confused by the List generic class or some thing right? – Sana.91 May 24 '12 at 07:21
0

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I don't understand what you mean. It looks like a code to me. – Darin Dimitrov May 24 '12 at 07:20
  • @DarinDimitrov i think its the metadata(on F12) published for the class he is talking about – V4Vendetta May 24 '12 at 07:22
  • Kindly read the question again ... I am just asking about the purpose of **IList List** property in Collection Base class ... That is a .net class.. the question has no code – Sana.91 May 24 '12 at 07:23
  • 1
    The purpose is to define a property called `List` which is of type `IList`. And the name of the property `List` has nothing to do with the `List` generic class and shouldn't be confused with. It's just a name. Could have been called anything. – Darin Dimitrov May 24 '12 at 07:24
  • If this is in the metadata then the implementation would be a private set, which you are not seeing from the outside – benPearce May 24 '12 at 07:26