convert List<List<object>> to IList<IList<object>>

这一生的挚爱 提交于 2019-11-28 18:46:43

You can't perform that conversion via straight casting - it wouldn't be safe. Instead, you should use:

IList<IList<object>> ret = new List<IList<object>>();

Then for each "sublist" you can use:

// Or whatever
ret.Add(new List<object>());

Finally, just return ret.

You could use LINQ to perform the conversion of your existing List<List<object>> when you return it - but it would be better to just create a more appropriate type to start with, as shown above.


To understand why some of the existing answers are wrong, suppose you could do this:

IList<IList<object>> p = new List<List<object>>();

Then this would be valid:

List<List<object>> listOfLists = new List<List<object>>();
IList<IList<object>> p = listOfLists;
p.Add(new object[]);
List<object> list = p[0];

But p[0] is a reference to an object[], not a List<object>... our supposedly type-safe code doesn't look as safe any more...

Fortunately, IList<T> is invariant to prevent exactly this problem.

var myOriginalList = new List<List<Object>>();
var converted = ((IList<IList<Object>>)myOriginalList.Cast<IList<Object>>().ToList()); // EDIT: Added .ToList() here

You shouldn't need to convert back- you can do just about anything on IList that you could on List.

You would have to declare your list as

IList<IList<object>> list = new List<IList<object>>(); // Works!

This works, because only the outer list is created in the first place. You can then insert individual items that are compatible with IList<object>:

list.Add(new List<object>());
list.Add(new object[10]);
public IList<IList<object>> Fetch(string data)
{
  IList<IList<object>> p = new List<IList<object>>();

  // add your stuff here

  return p;
}

You need to change the declaration of the result variable from List<List<object> to IList<IList<object>>

Which you can instantiate against List<IList<object>>

And each item in the result can be of type List<object>

    static IList<IList<object>> Test()
    {
        IList<IList<object>> result = new List<IList<object>>();
        var item = new List<object>() { "one", "two", "three" };
        result.Add(item);
        return result;
    }

List<T> and IList<T> do not support covariance. So this does not compile:

List<List<string>> list = new List<IList<string>>(); // DOES NOT COMPILE!!!!!
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!