可能重复:
在循环内部还是外部声明变量更好?
Resharper 希望我改变这一点:
int Platypus;
string duckBill1;
string duckBill2;
string duckBill3;
. . .
using (OracleDataReader odr = ocmd.ExecuteReader()) {
while (odr.Read()) {
Platypus = odr.GetInt32("Platypus");
duckBill1 = odr.GetString("duckBill1");
duckBill2 = odr.GetString("duckBill2");
duckBill3 = odr.GetString("duckBill3");
switch (Platypus) {
. . .
...对此:
using (OracleDataReader odr = ocmd.ExecuteReader()) {
while (odr.Read()) {
int Platypus = odr.GetInt32("Platypus");
string duckBill1 = odr.GetString("duckBill1");
string duckBill2 = odr.GetString("duckBill2");
string duckBill3 = odr.GetString("duckBill3");
switch (Platypus) {
. . .
...但是以这种方式(至少看起来),变量被声明 N 次,每次通过 while 循环一次。Resharperized 方式真的比原来的更好吗?