B. *_*non 8 c# resharper optimization
可能重复:
在循环内部或外部声明变量是否更好?
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) {
. . .
Run Code Online (Sandbox Code Playgroud)
......对此:
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) {
. . .
Run Code Online (Sandbox Code Playgroud)
......但是以这种方式(似乎,至少,似乎),vars被声明N次,每次通过while循环一次.Resharperized方式真的比原来好吗?
rsb*_*rro 16
是的,它更好,因为你限制了声明变量的范围.在循环内声明它们不会对性能产生影响.Resharper建议这种改变的原因是你没有在循环之外使用它们.
一般来说,在尽可能窄的范围内声明变量是一种很好的编程习惯.原因是:
即使看起来变量是在循环中每次迭代时新声明的,但它们是在编译时声明的,而不是运行时.在变量[s]的堆栈帧上分配空间,并且在循环上的每次迭代中重用相同的空间.
| 归档时间: |
|
| 查看次数: |
1027 次 |
| 最近记录: |