From: Tony Johansson on
Hi

I know that IList<T> support IList because we can write as I have done in
main below.
But why is not this documented in MSDN.
I have listed all the relevant interface declaration below and there no sign
of that IList<T> support IList.
I had hopped to see something like public interface IList<T> : IList

public static void Main()
{
List<string> stringList = new List<string>();
stringList.Add("Test");
IList theList = (IList)stringList;
object firstItem = theList[0];
}

public interface IList<T> : ICollection<T>,
IEnumerable<T>, IEnumerable

public interface ICollection<T> : IEnumerable<T>,
IEnumerable

public interface IEnumerable<T> : IEnumerable

[ComVisibleAttribute(true)]
[GuidAttribute("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerable

//Tony


From: Stefan Hoffmann on
hi Tony,

On 11.02.2010 21:24, Tony Johansson wrote:
> I know that IList<T> support IList because we can write as I have done in
> main below.
> But why is not this documented in MSDN.
> I have listed all the relevant interface declaration below and there no sign
> of that IList<T> support IList.
Your mixing it up...

> public static void Main()
> {
> List<string> stringList = new List<string>();
> stringList.Add("Test");
> IList theList = (IList)stringList;
Your using List<T>, not IList<T>:

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

[SerializableAttribute]
public class List<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable

mfG
--> stefan <--
From: Peter Duniho on
Tony Johansson wrote:
> Hi
>
> I know that IList<T> support IList because we can write as I have done in
> main below.

As Stefan points out, your code example doesn't show what you claim it
shows.

In fact, if you wrote it correctly per the example you're claiming you
posted, it wouldn't compile:

public static void Main()
{
IList<string> stringList = new List<string>();
stringList.Add("Test");
IList theList = (IList)stringList;
object firstItem = theList[0];
}

As for why IList<T> doesn't inherit IList, it's because IList<T> is not
covariant (in fact, it's invariant). That is, you can't substitute
sub-classes in the type argument within the interface.

IList would essentially be IList<System.Object> and since IList has
methods that are both input and output, that can't be allowed. If it
were, you could write something like this:

public static void Main()
{
IList<string> stringList = new List<string>();

stringList.Add("Test");

IList theList = (IList)stringList;

theList[0] = new object();
}

Which would result in an object being present in your List<string> that
is not in fact of type string. Very bad.

Pete
From: Peter Duniho on
Peter Duniho wrote:
> [...]
> In fact, if you wrote it correctly per the example you're claiming you
> posted, it wouldn't compile:
>
> public static void Main()
> {
> IList<string> stringList = new List<string>();
> stringList.Add("Test");
> IList theList = (IList)stringList;
> object firstItem = theList[0];
> }

Minor correction. The above would in fact compile, because it's got an
explicit cast. But it would throw an exception when executed.

A more correct code example for the demonstration being claimed wouldn't
even compile:

public static void Main()
{
IList<string> stringList = new List<string>();
stringList.Add("Test");
IList theList = stringList;
object firstItem = theList[0];
}
From: Kenneth Cochran on
On 2/11/2010 3:24 PM, Tony Johansson wrote:
> Hi
>
> I know that IList<T> support IList because we can write as I have done in
> main below.

IList<T> doesn't inherit IList, but List<T> does. In fact all
collections in System.Collections.Generic that implement IList<T> also
implement IList. If you were to create your own implementation of
IList<T> without inheriting IList it would generate an
InvalidCastException.

> But why is not this documented in MSDN.
> I have listed all the relevant interface declaration below and there no sign
> of that IList<T> support IList.
> I had hopped to see something like public interface IList<T> : IList
>
> public static void Main()
> {
> List<string> stringList = new List<string>();
> stringList.Add("Test");
> IList theList = (IList)stringList;
> object firstItem = theList[0];
> }
>
> public interface IList<T> : ICollection<T>,
> IEnumerable<T>, IEnumerable
>
> public interface ICollection<T> : IEnumerable<T>,
> IEnumerable
>
> public interface IEnumerable<T> : IEnumerable
>
> [ComVisibleAttribute(true)]
> [GuidAttribute("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
> public interface IEnumerable
>
> //Tony
>
>

 |  Next  |  Last
Pages: 1 2
Prev: f
Next: TcpClient buffer size limit?