[quoted text, click to view] Lucian Wischik <lu.nn@wischik.com> wrote:
> 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);
Could you post a short but complete program that demonstrates that?
Here's one which seems to go against it:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class Test
{
static void Main()
{
List<string> xs = new List<string>();
ReadOnlyCollection<string> ro =
new ReadOnlyCollection<string>(xs);
IList<string> il = ro as IList<string>;
Console.WriteLine (il != null);
}
}
That prints out "True" as expected.
[quoted text, click to view] > 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");
No, it shouldn't - ReadOnlyCollection implements IList<T>.Insert
explicitly, so it's only "there" when you're using the
ReadOnlyCollection as an IList<T>. In other words, you can do:
((IList<T>)ro).Insert (0, "hello");
[quoted text, click to view] > 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.
Search for "explicit interface implementation". Basically, instead of:
public void MethodName(...);
you do
void InterfaceName.MethodName(...);
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet