Search code examples
c#member-hiding

C# List<T> hides properties?


Possible Duplicate:
How does List<T> make IsReadOnly private when IsReadOnly is an interface member?

Okay, this is driving me nuts. List<T> implements IList<T>. However,

IList<int> list = new List<int>();
bool b = list.IsReadOnly;
bool c = ((List<int>)list).IsReadOnly;    // Error

The error is:

'System.Collections.Generic.List' does not contain a definition for 'IsReadOnly' and no extension method 'IsReadOnly' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

How can this be? Doesn't this violate the very rule that we tell everyone, about not hiding members? What is the implementation detail here?


Solution

  • Because the implementation is via an explicit interface implementation.

    Meaning that it's defined as

    bool IList<T>.IsReadOnly { get; set; //etc }
    

    http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx

    And that is why it's not off of List.