views:

52

answers:

1
+1  Q: 

Indexer on IList

This is probably something I'm overlooking in C# inheritance, but I was trying to build a class that implements IList(The System.Collections version, not generics) The compiler complained that I didn't implement an indexer, but I was looking at CollectionBase, which also implements IList, and it doesn't appear to expose an indexer either.

In looking at it through Reflector it seems that CollectionBase is abstract and that it does implement an indexer, but it appears to be private. I tried this and no dice, the compiler insists that if I want to implement IList, I have to have a public, non-static indexer that returns an object.

So my question is not why do I need one, but how is that CollectionBase gets away with a private indexer?

+2  A: 

It doesn't- it uses explicit interface implementation:

object IList.this[int index] { get; set; }

It's there, you just don't see it on the default class interface (to use a COM term)- you have to cast to IList to see/use it.

nitzmahone
ah, ok, thanks. I always forget about explicit inheritance. I wonder sometimes if its only there to get around not having multiple inheritance and avoiding name collisions.
LoveMeSomeCode