在 C# 中使用 return 和简写 if

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1392666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:59:57  来源:igfitidea点击:

Using return and short-hand if in C#

c#syntax

提问by Mr. Smith

Why wouldn't the following line of code work in a method?

为什么以下代码行不能在方法中工作?

return (count > 0) ? true : false;

It works perfectly fine if I do:

如果我这样做,它工作得很好:

bool ret = (count > 0) ? true : false;
return ret;

Bonus Question: Is it really faster or more effective than the standard if statement?

额外问题:它真的比标准 if 语句更快或更有效吗?

bool ret = false;
if(count > 0)
    ret = true;
return ret;

Which one would you recommend?

你会推荐哪一个?

采纳答案by Jon Skeet

I would recommend:

我会推荐:

return count > 0;

There's no need to explicitly return trueor false.

无需显式返回trueor false

Having said that, your compilation error intrigues me. At first sight it looks like it should work. Could you post a short but complete example that fails to compile? The type of that conditional expression should be boolwith no problems. My guess is you've got a more complicated scenario, and by simplifying the example you've removed the real problem.

话虽如此,您的编译错误引起了我的兴趣。乍一看,它看起来应该可以工作。您能否发布一个无法编译的简短但完整的示例?该条件表达式的类型应该bool没有问题。我的猜测是你有一个更复杂的场景,通过简化示例你已经消除了真正的问题。

As for the bonus question: I don't know which would be faster, nor do I care in 99.99% of cases. I'd be amazedto find that it caused any significant delay, unless it prohibited inlining for some reason. Go for the most readablesolution - which is the simple return statement, IMO.

至于奖金问题:我不知道哪个会更快,在 99.99% 的情况下我也不关心。我会惊讶地发现它造成了任何显着的延迟,除非它出于某种原因禁止内联。寻找最易读的解决方案 - 这是简单的 return 语句,IMO。

回答by Makach

try this:

尝试这个:

return count > 0;

before return returns the expression count > 0 is evaluated and gives true or false.

在 return 返回之前,表达式 count > 0 被评估并给出 true 或 false。

this should also work:

这也应该有效:

return (count > 0 ? true : false); 

but I'd recommend you didn't do this.

但我建议你不要这样做。

I always try to keep the amount of horizontal operations low, I believe it makes it easier to read code.

我总是尽量减少水平操作的数量,我相信这会让代码更容易阅读。

just imagine the following scenario which will just confuse :)

想象一下下面的场景,这只会让人感到困惑:)

return count > 0 ? false : true; 

回答by sindre j

this works

这有效

return (count > 0 ? true : false);

You can then make it return other values than true and false. In your particular case I would do like the other suggestions; return count > 0;

然后,您可以使其返回 true 和 false 以外的其他值。在您的特定情况下,我会喜欢其他建议;返回计数> 0;

回答by ema

From the point of view of C#

从 C# 的角度来看

return count > 0;

is better for it's readabilty.

更好的是它的可读性。

But the compiler optmize the code, so your three options are actually the same once compiled. You could try to look at the IL code to verify!

但是编译器会优化代码,因此您的三个选项在编译后实际上是相同的。您可以尝试查看IL代码进行验证!