可能重複:
why can't we define a variable inside a while loop?在`while`循環中,爲什麼我不能在`for`循環中聲明條件變量?
我想簡化下面的代碼:
string line;
while ((line = sr.ReadLine()) != null)
到:
while ((string line = sr.ReadLine()) != null)
但失敗。
但是,我們一定可以在for
做到這一點:
for (int i=0, int n=50;i<n;i++)
http://stackoverflow.com/questions/1581364/why-cant-we-define-a-variable-inside-a-while-loop – 2011-08-21 22:54:13