Unable to convert List<List<int>> to return type IList<IList<int>>

▼魔方 西西 提交于 2020-08-19 11:41:56

问题


For level order traversal why does this exception occur? Following exception occurs:

Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.List<int>>' to 'System.Collections.Generic.IList<System.Collections.Generic.IList<int>>'. An explicit conversion exists (are you missing a cast?)

public IList<IList<int>> LevelOrder(TreeNode root) 
{
    var result = new List<List<int>>();
    var que = new Queue<TreeNode>();

    //if(root==null) return result;

    que.Enqueue(root);
    while(que.Count!=0)
    {
        int n = que.Count;
        var subList = new List<int>();
        for(int i=0;i<n;i++)
        {
            if(que.Peek().left!=null) 
                que.Enqueue(que.Peek().left);
            if(que.Peek().right!=null)
                que.Enqueue(que.Peek().right);
            subList.Add(que.Dequeue().val);
        }
        result.Add(subList);
    }
    return  result;
}

回答1:


Just change the declaration of your result to List<IList<int>>.

List<T> implements IList<T>, but List<List<T>> does not implement IList<IList<int>>. Generic parameters are not covariant or contravariant unless defined that way and IList<T> is not, so the type must match exactly.

public IList<IList<int>> LevelOrder(TreeNode root)
{
    var result = new List<IList<int>>();
    var que = new Queue<TreeNode>();

    //if(root==null) return result;

    que.Enqueue(root);
    while (que.Count != 0)
    {
        int n = que.Count;
        var subList = new List<int>();
        for (int i = 0; i < n; i++)
        {
            if (que.Peek().left != null)
                que.Enqueue(que.Peek().left);
            if (que.Peek().right != null)
                que.Enqueue(que.Peek().right);
            subList.Add(que.Dequeue().val);
        }
        result.Add(subList);
    }
    return result;
}



回答2:


Pretty sure that if it compiles, doing a cast is a real bad idea. Here's why:

public class myStupidList : IList<int>
{
//implementation unimportant
}

private void Button_Click(object sender, RoutedEventArgs e)
{
  var result = new List<List<int>>();
  IList<IList<int>> imNotAListofLists = (IList<IList<int>>)result;
  imNotAListofLists.Add(new myStupidList());
  //result is not a very valid variable right now, is it?
}

As mentioned in my comment, these types of collection issues boil down to covariance and contravariance, and .NET does provide a lot of tools for dealing with them. (Such as various read-only collections and interfaces)

..Which, explains why you get your error as well. There is no implicit cast from List<List> to List<IList> because that cast cannot succeed without breaking type-safety. (and as @Grax mentioned, neither derives from the other)




回答3:


Bellow code will work :-

List<IList<int>> result = new List<IList<int>>();

or

var result = new List<IList<int>>();



来源:https://stackoverflow.com/questions/48574660/unable-to-convert-listlistint-to-return-type-ilistilistint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!