我想知道在C#中使用ifs时是否存在性能差异,它们是否嵌套.这是一个例子:
if(hello == true) {
if(index == 34) {
DoSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
这比这更快还是慢:
if(hello == true && index == 34) {
DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
更好的是
if(SomethingShouldBeDone()) {
DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
......同时在城市的另一个地方......
private bool SomethingShouldBeDone()
{
return this.hello == true && this.index == 34;
}
Run Code Online (Sandbox Code Playgroud)
在99%的现实生活中,这对性能影响很小或没有,并且只要你有意义地命名,它就会更容易阅读,理解和(因此)维护.
| 归档时间: |
|
| 查看次数: |
2125 次 |
| 最近记录: |