Why does var evaluate to System.Object in "foreach (var row in table.Rows)"?
Posted
by DanM
on Stack Overflow
See other posts from Stack Overflow
or by DanM
Published on 2010-05-07T06:49:22Z
Indexed on
2010/05/07
6:58 UTC
Read the original article
Hit count: 132
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?
© Stack Overflow or respective owner