L
Lucian Wischik
Does ReadOnlyCollection<T> really implement IList<T>, like it claims
to do? ...
When I right-click on ReadOnlyCollection and look at its definition,
it says this:
public class ReadOnlyCollection<T>
: IList<T>,ICollection<T>,IEnumerable<T> ...
However, the following code fails its assertion check:
List<string> xs = new List<string>();
ReadOnlyCollection<string> ro = new ReadOnlyCollection<string>(xs);
IList<string> il = ro as IList<string>;
Debug.Assert(il != null);
I don't understand how this is possible. If ReadOnlyCollection<T>
claims to implement IList<T>, then I should be able to cast to it,
shouldn't I?
In a similar vein, the following code fails to compile because, it
claims, ReadOnlyCollection<string> doesnt implement the Insert method.
How is this possible? Insert is part of IList<T>, which
ReadOnlyCollection<T> inherits from, so it should be there!
ro.Insert(0,"hello");
I'm asking these questions because I want to create my own
wrapper-class, similar to how ReadOnlyCollection is a wrapper around
an IList. I need to understand which interfaces I should declare
myself as providing. And so I need to understand why
ReadOnlyCollection declares that it supports the interfaces it does,
and how.
I wonder if ReadOnlyCollection really does implement those interfaces,
but somehow *hides* the Insert method &c.? How is this done in C#? The
documentation specifically says that methds from interfaces must be
public, so I don't know.
to do? ...
When I right-click on ReadOnlyCollection and look at its definition,
it says this:
public class ReadOnlyCollection<T>
: IList<T>,ICollection<T>,IEnumerable<T> ...
However, the following code fails its assertion check:
List<string> xs = new List<string>();
ReadOnlyCollection<string> ro = new ReadOnlyCollection<string>(xs);
IList<string> il = ro as IList<string>;
Debug.Assert(il != null);
I don't understand how this is possible. If ReadOnlyCollection<T>
claims to implement IList<T>, then I should be able to cast to it,
shouldn't I?
In a similar vein, the following code fails to compile because, it
claims, ReadOnlyCollection<string> doesnt implement the Insert method.
How is this possible? Insert is part of IList<T>, which
ReadOnlyCollection<T> inherits from, so it should be there!
ro.Insert(0,"hello");
I'm asking these questions because I want to create my own
wrapper-class, similar to how ReadOnlyCollection is a wrapper around
an IList. I need to understand which interfaces I should declare
myself as providing. And so I need to understand why
ReadOnlyCollection declares that it supports the interfaces it does,
and how.
I wonder if ReadOnlyCollection really does implement those interfaces,
but somehow *hides* the Insert method &c.? How is this done in C#? The
documentation specifically says that methds from interfaces must be
public, so I don't know.