Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

I'm creating a specialised proxy class that implements IList<T> and wraps an internal List<T> instance.

List<T> itself implements IList<T>, which declares a member bool IsReadOnly, but when I try to access that member from my own class, I can't because in List<T>, IsReadOnly is private.

So my question is; if an implementation of an interface requires all implemented members to be public, why does List<T> get to implement IsReadOnly as private and thus deny me access to it?

like image 850
Nathan Ridley Avatar asked May 02 '09 13:05

Nathan Ridley


3 Answers

It implements the interface member explicitly.

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

Note that this does not make the interface member private. It's still available publicly, but only if you look at the object through the interface (with casting).

like image 126
mmx Avatar answered Nov 11 '22 16:11

mmx


The reason it's able to do this is that it uses explicit interface implementation.

bool IList<T>.IsReadOnly { get { ... } }

This member is still accessible, but it can only be accessed via the IList<T> type.

List<T> list = ...;
bool isReadOnly = ((IList<T>)list).IsReadOnly;

When implementing IList<T>, the type has not promised to provide an IsReadOnly value itself. Instead it's promised to provide an IsReadOnly property when viewed as an IList<T>. Whether or not it provides it on the actual class is purerly a choice of the type author.

like image 17
JaredPar Avatar answered Nov 11 '22 16:11

JaredPar


For the case if List<T>, IsReadOnly doesn't really make sense: it's always false. List therefore implements the ICollection<T> (and the IList) interface explicitly, thus making the interface “private” for normal use. If you need to access it anyway, you can through an explicit cast to the interface:

bool result = ((ICollection<string>)yourStringList).IsReadOnly;
// Or equivalently using `IList`.
like image 6
Konrad Rudolph Avatar answered Nov 11 '22 15:11

Konrad Rudolph