views:

52

answers:

5

Looking at the interface System.Collections.Generic.ICollection its definition requires that the inheriting member contains the property bool IsReadOnly { get; }.

However I then took a look at the class System.Collections.Generic.List which inherits System.Collections.Generic.ICollection and this class does not contain a definition of bool IsReadOnly { get; }. How is the inheritance chain broken or am I missing something?

+1  A: 

The IsReadOnlyproperty is there, but List<T>is implementing it explicitly.

To convince yourself of this, you can do:

List<T> genericList = new List<T>();
IList explicitIList = genericList;

bool isReadOnly = explicitIList.IsReadOnly;

This should compile.

You might also want to look at this question and this article on how to implement interfaces explicitly, and how to refer to an explicitly implemented member on a type from outside the type.

Ani
+1  A: 

It is in the IList section:

IList implements ICollection

    public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
    {
        public List();
        public List(int capacity);
        public List(IEnumerable<T> collection);
        public int Capacity { get; set; }

        #region IList Members

        int IList.Add(object item);
        bool IList.Contains(object item);
        void ICollection.CopyTo(Array array, int arrayIndex);
        int IList.IndexOf(object item);
        void IList.Insert(int index, object item);
        void IList.Remove(object item);
        bool IList.IsFixedSize { get; }
        bool IList.IsReadOnly { get; }
        bool ICollection.IsSynchronized { get; }
        object ICollection.SyncRoot { get; }
        object IList.this[int index] { get; set; }

        #endregion

...and so on

}
CRice
+1  A: 

The member is implemented explicitly:

http://msdn.microsoft.com/en-us/library/bb346454.aspx

stephbu
A: 

Yes. It is implemented explicitly. So you shoud access its members in such way(explicitly casting it to interface) ((ICollection)list).IsReadOnly;

Dima
A: 

From the disassemble code in .NET reflector of System.Collections.Generic.List, it does contain IsReadOnly property.

 bool ICollection<T>.IsReadOnly { get; }
Danny Chen