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

Pos*_*Guy 15 c# asp.net

我试图使用该控件从.aspx设置我的.ascx控件的属性.

所以在我的.aspx之一中有这个控件,我有以下代码试图设置我的嵌入式.ascx的ItemsList属性:

Item item = GetItem(itemID);
myUsercontrol.ItemList = new List<Item>().Add(item);
Run Code Online (Sandbox Code Playgroud)

我正在尝试设置的.ascx中的属性如下所示:

public List<Item> ItemsList
{
   get { return this.itemsList; }
   set { this.itemsList = value; }
}
Run Code Online (Sandbox Code Playgroud)

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

所以我不知道它作为财产的一部分而变得无效吗?......很奇怪.

Mar*_*ers 36

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

mycontrol.ItemList = new List<Item>();
mycontrol.ItemList.Add(item);
Run Code Online (Sandbox Code Playgroud)

或使用集合初始化程序:

mycontrol.ItemList = new List<Item> { item };
Run Code Online (Sandbox Code Playgroud)