Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么类型推断与SelectedItemCollection中断?_C#_Asp.net_Devexpress - Fatal编程技术网

C# 为什么类型推断与SelectedItemCollection中断?

C# 为什么类型推断与SelectedItemCollection中断?,c#,asp.net,devexpress,C#,Asp.net,Devexpress,aspxcheckbox列表有一个名为“SelectedItems”的集合,类型为“SelectedItemCollection”(它实现ICollection,IEnumerable) 出于某种原因,这两者之间存在差异: foreach (ListEditItem si in cblCommonJointOwnership.SelectedItems) { //do stuff and have si.Value and si.Text and si.Foo } 这是: foreac

aspxcheckbox列表有一个名为“SelectedItems”的集合,类型为“SelectedItemCollection”(它实现ICollection,IEnumerable)

出于某种原因,这两者之间存在差异:

foreach (ListEditItem si in cblCommonJointOwnership.SelectedItems)
{
    //do stuff and have si.Value and si.Text and si.Foo
}
这是:

foreach (var si in cblCommonJointOwnership.SelectedItems)
{
   //seems that si is just a plain object here...
}
我意识到这不是DevExpress特有的。。因此,我假设一个一般性的问题:“var”版本为什么会被推断为对象而不是listedItem


谢谢

,因为
SelectedItems
是一个专门的收藏。它也不是通用的。这使得类型推断无法操作


换句话说,列表中的对象可以是任何东西;甚至是不同类型的。因此,集合不提供任何类型化结果。

SelectedItemCollection
实现
IEnumerable
,但不实现
IEnumerable
。因此,编译器对集合中存储的类型一无所知,必须采用最通用的类型(即
Object

您的第一条语句指定了类型。这实际上会导致将集合中的每个项强制转换回指定的类型,并且如果集合中的任何元素无法正确强制转换,则将失败