[英]cannot assign void to implicitly-typed local variable
Not sure what I am doing wrong syntax-wise here 不知道我在这里做错了语法
var inventories = new List<Inventory>().Add(new Inventory
{
});
Compile-time error: 编译时错误:
cannot assign void to implicitly-typed local variable
无法为隐式类型的局部变量分配void
List.Add
returns void
. List.Add
返回void
。 Try 尝试
var inventories = new List<Inventory>(){ new Inventory() };
The return value of Add
is void
. Add
的返回值是void
。
I think you meant to write: 我想你打算写:
var inventories = new List<Inventory>();
inventories.Add(new Inventory{
});
The return type of Add()
is a void, ie no return value, your code is trying to assign a reference to "nothing" Add()
的返回类型为空,即没有返回值,您的代码正在尝试分配对“ nothing”的引用
You have two options, declare the list, and then add to it 您有两个选择,声明列表,然后添加到列表中
var inventories = new List<Inventory>();
inventories.Add(new Inventory());
or use an array initializer 或使用数组初始化器
var inventories = new List<Inventory>()
{
new Inventory()
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.