C#二维数组的初始化(IList<IList<int>>的初始化)

当输出为二维数组的时候,通常LeetCode需要的输出类型为IList<IList<int>>,这个时候,可能会遇到以下错误:

  IList<IList<int>> ans = new IList<IList<int>>();  // 无法创建抽象类或接口"IList<IList<int>>"的实例
  IList<IList<int>> ans = new List<List<int>>(); 
//无法将类型"System.Collections.Generic.List < System.Collctions.Generic.List <int> >"隐式转换为
//"System.Collctions.Generic.lList<System.Collctions.Generic.lList<int>>".存在一个显式转换(是否缺少强制转换?)

以上两种写法都会导致编译器报错。
正确的写法应该是

IList<IList<int>> ans = new List<IList<int>>();  // 通过
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。