IList<T> and IList


T

Tony Johansson

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
 
Ad

Advertisements

S

Stefan Hoffmann

hi Tony,

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 <--
 
P

Peter Duniho

Tony said:
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
 
P

Peter Duniho

Peter said:
[...]
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];
}
 
K

Kenneth Cochran

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
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
 
K

Kenneth Cochran

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

Actually it does compile and run because List<T> implements IList as well.
 
Ad

Advertisements

P

Peter Duniho

Kenneth said:
Actually it does compile and run because List<T> implements IList as well.

Ah, of course. How careless of me. That's what I get for failing to
post the correct code example in the first place. I lose track of what
the code is doing.

Note that an assignment/addition via the List<T> implementation of IList
would fail unless the type of the object is of the correct type. Thus,
the explicit cast succeeds, but adding an object of the wrong type (as
in my second example in that post) will fail at run-time.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top