两个如果条件或一个如果与OR

Ram*_*Ram 1 .net c# optimization

我有一个小疑问

我有以下代码

bool res= false;
if(cond1)
{
   res = true;
}

if(cond2)
{
   res = true;
}
Run Code Online (Sandbox Code Playgroud)

如果我放下以下代码,而不是这个

if(cond1 || cond2)
{
   res = true;
}
Run Code Online (Sandbox Code Playgroud)

哪个代码段会更优化?

我相信这将是第二个,因为我避免了If条件.

Fai*_* S. 9

好吧,我不知道它是否合适但是:在这种情况下并不重要.

不要过多的微优化.

但要回答你的问题,坚持使用可读的问题.

最快的解决方案是这样的:

bool res = cond1 || cond2;
Run Code Online (Sandbox Code Playgroud)


Dar*_*rov 8

这是微优化.选择一个更具可读性的:

return cond1 || cond2;
Run Code Online (Sandbox Code Playgroud)