Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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
为什么';t类型推断(var)是否使用ASP.NET GridView.Rows属性?_Asp.net_Visual Studio 2008 - Fatal编程技术网

为什么';t类型推断(var)是否使用ASP.NET GridView.Rows属性?

为什么';t类型推断(var)是否使用ASP.NET GridView.Rows属性?,asp.net,visual-studio-2008,Asp.net,Visual Studio 2008,在这个代码片段中 foreach (var row in grid.Rows) { } 行的类型被推断为对象,而不是GridViewRow。我必须显式声明要使用GridViewRow的类型: foreach (GridViewRow row in grid.Rows) { } GridView.Rows返回GridViewRowCollection,集合上的GetEnumerator“返回包含GridViewRowCollection中所有GridViewRow对

在这个代码片段中

foreach (var row in grid.Rows) { }              
行的类型被推断为对象,而不是GridViewRow。我必须显式声明要使用GridViewRow的类型:

foreach (GridViewRow row in grid.Rows) { }
GridView.Rows返回GridViewRowCollection,集合上的GetEnumerator“返回包含GridViewRowCollection中所有GridViewRow对象的枚举数。”如果我通过索引访问集合中的一行

var row = grid.Rows[0];
已正确推断类型。为什么编译器不能在foreach声明中推断行的类型


编辑添加:有趣的回答,我不认为要寻找明显的问题。ListView.Items实现了
IList
,所以这看起来像是我的疏忽。

没有实现
IEnumerable
,因为
GridViewRowCollection
实现了非泛型的
IEnumerable
ICollection
接口,它们不是这样的强类型,但是它的
Item
属性是强类型的,并返回
GridViewRow
。微软真棒

如果他们不匆忙完成所有这些版本,他们可能有时间实际实现所有这些缺失的接口:|