I understand that implicitly-typed local variables must be initialized
.
I know that result
will be an IList
so could I somehow say that var result
will be an IList
?
var result; //initialize to something
if( x < 0)
{
result = (from s in context.someEntity
where s.somecolumn = x
select new { c1 = s.c1,c2=s.c2}).ToList();
}
if(x >= 0)
{
result = (from s in context.someEntity
where s.someOtherColumn = x
select new { c1 = s.c1,c2=s.c2}).ToList();
}
foreach(var y in result)
{
//do something . UPDATE 1: Retrieve y.c1, y.c2
}
If you know you want it to be an
IList
, why not just declare it as anIList
?Using
var
for uninitialized variables is (IMO) pretty unreadable.