Why does var evaluate to System.Object in "foreach (var row in table.Rows)"?
- by DanM
When I enter this foreach statement...
foreach (var row in table.Rows)
...the tooltip for var says class System.Object
I'm confused why it's not class System.Data.DataRow.
(In case you're wondering, yes, I have using System.Data at the top of my code file.)
If I declare the type explicitly, as in...
foreach (DataRow row in table.Rows)
...it works fine with no errors.
Also if I do...
var numbers = new int[] { 1, 2, 3 };
foreach (var number in numbers)
...var evaluates to struct System.Int32. So, the problem is not that var doesn't work in a foreach clause.
So, there's something strange about DataRowCollection where the items don't automatically evaluate to DataRow. But I can't figure out what it is. Does anyone have an explanation?