Reputation: 6518
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?
Upvotes: 0
Views: 530
Reputation: 113402
The IsReadOnly
property 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.
Upvotes: 1
Reputation: 43503
From the disassemble code in .NET reflector of System.Collections.Generic.List, it does contain IsReadOnly
property.
bool ICollection<T>.IsReadOnly { get; }
Upvotes: 0
Reputation: 1
Yes. It is implemented explicitly. So you shoud access its members in such way(explicitly casting it to interface) ((ICollection)list).IsReadOnly;
Upvotes: 0
Reputation: 5082
The member is implemented explicitly:
http://msdn.microsoft.com/en-us/library/bb346454.aspx
Upvotes: 1
Reputation: 12567
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
}
Upvotes: 1