我明白implicitly-typed local variables must be initialized
。初始化隱式鍵入的局部變量爲IList
我知道result
將是一個IList
所以我可以不知何故說var result
將是一個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
}
我通常同意你的看法,但我見過一些人使用它,所以如果他們改變了類型名稱(例如從人類到人物),他們不必更改聲明。儘管大多數重構工具毫無意義 – taylonr 2011-04-20 11:43:16