C# 3.0 introduced the var keyword. And when compiled, the compiler will insert the right types for you. This means that it will even work on a 2.0 runtime. So far so good. But the other day I found a case where, the var keyword would be replaced with just object and thus not specific enough. Say you have something like:
var data = AdoMD.GetData(...); // GetData returns a DataTable foreach (var row in data.Rows) { string blah = (string)row[0]; // it fails since it's of type object }
When I try to use row both IntelliSense and the compiler tells me that it is of type object. data.Rows is of type System.Data.DataRowCollection. The following works:
var data = AdoMD.GetData(...); // GetData returns a DataTable foreach (DataRow row in data.Rows) { string blah = (string)row[0]; // works since it now has the right type }
This is not a question about the usage of var, there is a thread for that here.
I’m using Visual Studio 2008 SP1 btw.
Edit: correct code now attached.
I think I see the problem. DataRowCollection is non-generic and thus the only thing the compiler knows is that contains objects of type Object. If it had been a generic datastructure this would have worked.