c# - 无法转换 IList列出
问题描述
这是我的函数签名:
public IList<IList<int>> LevelOrder(TreeNode root)
这是显示我如何创建IList<IList<int>>
从函数返回的代码片段:
IList<IList<int>> result = new List<List<int>>();
result.Add(new List<int>());
while (completeQueue.Count != 0)
{
current = completeQueue.Dequeue();
if (level != current.level)
{
result.Add(new List<int>());
level = current.level;
}
result[level].Add(current.node.val);
}
return result;
当我运行这个函数时,我从最后一行 ( return result
) 中得到一个错误:
Line 49: Char 36: error CS0266: 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?) (in Solution.cs)
List
实现IList
接口。为什么这不起作用?
解决方案
您返回 aIList<List<int>>
而不是 a IList<IList<int>>
。
这不是同一类型,因为您的List<List<int>>
工具IList<List<int>>
不是IList<IList<int>>
。
你应该使用:
var result = new List<IList<int>>();
推荐阅读
- javascript - JS 函数,只能被 new 关键字调用
- vbscript - VBScript 十进制除以整数返回额外的余数
- postman - 在 Postman 中使用变量设置 cookie
- sql - ORA-01422:- ORA-06512:
- laravel - Laravel request->all() 显示带有页面 url 的数组
- android - 在 setTargetFragment 中出现错误
- java - 无法解决 IntelliJ IDEA 2018.1 中的依赖关系
- sql-server - 检查命名的 SQL Server 实例是否在本地计算机上运行
- python - Python中的pyinstaller超过了最大递归深度?
- python - 致命的 Python 错误:(pygame 降落伞)分段错误不起作用