BruceHill BruceHill - 1 year ago 127
C# Question

Convert from IList<T> to non-generic IList

I am implementing

that requires a method
with the following signature:

IList GetList()

I am using .NET framework 2 and I'm wanting to return an object that implements IList as follows:

public System.Collections.IList GetList()
return this._mydata; // Implements IList<MyDataRow>

But I get a compile error saying:
Cannot implicitly convert type MyData to System.Collections.IList

If I create a new list of type
, populate it and return this list object, then it works. So in other words, this works:

public System.Collections.IList GetList()
List<MyDataRow> list = new List<MyDataRow>();
foreach (MyDataRow row in this._mydata)
return list;

But it seems very inefficient to have to recreate the list just to get it from type
. Why is it that I can return a
List<MyDataRow>' from 'GetList()
, but not an
? Does anyone know of a way for me to return the
without repopulating a new list?


member variable is declared:

private MyData _mydata;

is declared:

public class MyData : IList<MyDataRow>

Answer Source

Why is it that I can return a List<MyDataRow> from GetList(), but not an IList<MyDataRow>

This is because List<T> implements IList, IList<T> cannot be cast to IList they are 2 separate interfaces. So to answer your question:

Does anyone know of a way for me to return the IList<MyDataRow> without repopulating a new list?

If the concrete type implements IList (which List<T> does) then you can explicitly cast it e.g.

return (IList)this.mydata;


Based on your update, you will have to update MyData to implement IList otherwise you have no choice but to return a new collection which does implement it.

Alternatively, if MyData is indeed a generic list then I would suggest you have it inherit from List<T>, that way you get a lot more flexibility & compatibility out of the box e.g.

class MyData : List<MyDataRow>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download