简体   繁体   English

无法为隐式类型的局部变量分配void

[英]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.

相关问题 无法使用var和foreach将void分配给隐式类型的局部变量 - Cannot assign void to an implicitly-typed local variable with var and foreach 无法为WP 8.1中的隐式类型的局部变量分配void - Cannot assign void to an implicitly-typed local variable in WP 8.1 无法将void分配给隐式类型的局部变量 - Cannot assign void to an implicitly-typed local variable 错误无法将void分配给隐式类型的局部变量 - ERROR Cannot assign void to an implicitly-typed local variable 无法在单元测试中将 void 分配给隐式类型的变量 - Cannot assign void to an implicitly-typed variable in unit test Linq ForEach - 返回不能将&#39;void&#39;分配给隐式类型的局部变量 - Linq ForEach - Returning cannot assign 'void' to an implicitly-typed local variable 无法使用Entity Framework 6将空值分配给隐式类型的局部变量 - Cannot assign a void to an implicitly-typed local variable using Entity Framework 6 无法将方法组分配给隐式类型的局部变量 - Cannot assign method group to an implicitly-typed local variable 无法分配 <null> 到隐式类型的局部变量 - Cannot assign <null> to an implicitly-typed local variable 不能将 null 分配给隐式类型的变量 - Cannot assign null to an implicitly-typed variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM