C# 错误:无法将类型“void”隐式转换为“System.Collections.Generic.List”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1868348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:30:13  来源:igfitidea点击:

Error: Cannot implicitly convert type 'void' to 'System.Collections.Generic.List'

c#asp.net

提问by PositiveGuy

I am trying to set a property of my .ascx controls from an .aspx using that control.

我正在尝试使用该控件从 .aspx 设置我的 .ascx 控件的属性。

So in one of my .aspxwhich has this control in it, I have the following code trying to set the ItemsList property of my embedded .ascx:

因此,在其中包含此控件的.aspx之一中,我有以下代码尝试设置嵌入的 .ascx 的 ItemsList 属性:

Item item = GetItem(itemID);
myUsercontrol.ItemList = new List<Item>().Add(item);

The property in the .ascx that I'm trying to set looks like this:

我试图设置的 .ascx 中的属性如下所示:

public List<Item> ItemsList
{
   get { return this.itemsList; }
   set { this.itemsList = value; }
}

Error: Cannot implicitly convert type 'void' to 'System.Collections.Generic.List'

错误:无法将类型“void”隐式转换为“System.Collections.Generic.List”

So I don't get where it's getting void as part of the property?...weird.

所以我不明白它作为财产的一部分在哪里变得无效?......奇怪。

采纳答案by Mark Byers

You can't do that because the Add function returns void, not a reference to the list. You can do this:

您不能这样做,因为 Add 函数返回 void,而不是对列表的引用。你可以这样做:

mycontrol.ItemList = new List<Item>();
mycontrol.ItemList.Add(item);

or use a collection initializer:

或使用集合初始值设定项

mycontrol.ItemList = new List<Item> { item };

回答by shahkalpesh

ItemListt.ItemList = new List<Item>().Add(item);

Does Addmethod return an instance of a list based class?

Add方法是否返回基于列表的类的实例?

EDIT: No, see this linkfor documentation on List<T>.Add

编辑:不,有关文档,请参阅此链接List<T>.Add

EDIT2: Try writing this piece of code & see what is the return value of Addmethod.

EDIT2:尝试编写这段代码并查看Add方法的返回值是什么。

List<Item> items = new List<Item>();
var modifiedList = items.Add(myItem);

You will see that, the code should fail at Addbecause it returns void.

您将看到,代码应该失败,Add因为它返回void.

回答by Lee

After creating the List<Item>you're immediately calling Add on it, which is a method returning void. This cannot be converted to the type of ItemList.ItemList.

创建之后,List<Item>您立即对其调用 Add,这是一个返回 void 的方法。这不能转换为 ItemList.ItemList 的类型。

You should do this instead:

你应该这样做:

var list = new List<Item>();
list.Add(item);
ItemList.ItemList = list;

回答by Scott Anderson

So long as ItemListis instantiated on your usercontrol, you can just do UserControl.ItemList.Add(item)

只要ItemList在您的用户控件上实例化,您就可以UserControl.ItemList.Add(item)

回答by Guffa

The Add method doesn't return a reference to the list. You have to do this in two steps:

Add 方法不返回对列表的引用。您必须分两步执行此操作:

ItemListt.ItemList = new List<Item>();
ItemListt.ItemList.Add(item);

Alternatively use a local variable to hold the reference before you put it in the property:

或者,在将引用放入属性之前,使用局部变量来保存引用:

List<Item> list = new List<Item>();
list.Add(item);
ItemListt.ItemList = list;

回答by mYsZa

new List<Item>().Add(item);

This line returns void.

此行返回无效。

Try:

尝试:

var list = new List<Item>();
list.Add(item);
ItemListt.ItemList = list;

回答by jeromej

For others who might come here from Googling the error:

对于可能通过谷歌搜索错误来到这里的其他人:

Sometimes it's because you didn't finish writing a line but it compiles anyway and the error only shows up on the next line which can be confusing/misleading if you don't pay attention. Eg:

有时是因为你没有写完一行,但它仍然编译,错误只出现在下一行,如果你不注意,这可能会造成混淆/误导。例如:

var foo = 33;
var bar = 

// Note that I never finished the assignation (got distracted, saved and it compiled)
//
// Some unrelated code returning "void" (another example would be List.Add)
await SomethingAsync(); // -> Error shows up here instead

Might save some of you all some headache trying to figure why "awaiting on my function" raises a "Cannot convert void to X", when really it's not the actual issue.

可能会让你们中的一些人头疼,试图弄清楚为什么“等待我的函数”会引发“无法将 void 转换为 X”,而实际上这不是实际问题。