views:

83

answers:

2

In this code fragment

foreach (var row in grid.Rows) { }

the type of row is inferred as Object instead of GridViewRow. I have to explicitly declare the type to work with a GridViewRow:

foreach (GridViewRow row in grid.Rows) { }

GridView.Rows returns a GridViewRowCollection and GetEnumerator on the collection "Returns an enumerator that contains all GridViewRow objects in the GridViewRowCollection." If I access a single row from the collection by index

var row = grid.Rows[0];

the type is correctly inferred. Why can't the compiler infer the type for row in the foreach declaration?

Edited to add: Interesting responses, I didn't think to look for the obvious problem. ListView.Items implements IList<ListViewDataItem>, so this looks like an oversight to me.

+3  A: 

Rows does not implement IEnumerable<GridViewRow>.

leppie
+3  A: 

Because GridViewRowCollection implements the non-generic IEnumerable and ICollection interfaces, which are not strongly-typed like this, but its Item property is strongly typed and returns a GridViewRow. Nice one Microsoft!

David M
Maybe if they didnt rush all these version they would have had time to actually implement all these missing interfaces :|
leppie