C# 编写“if”语句的不同方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/639166/
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-04 11:21:32  来源:igfitidea点击:

Different ways of writing the "if" statement

c#

提问by egyamado

I have seen different ways of writing an ifstatement.

我见过写if声明的不同方式。

Which one do you prefer and why?

你更喜欢哪一个,为什么?

Example 1:

示例 1:

if (val % 2 == 1){output = “Number is odd”;}else{output = “Number is even”;} 

Example 2:

示例 2:

if (val % 2 == 1)
{
    output = “Number is odd”;
}
else
{
   output = “Number is even”;
}

Example 3:

示例 3:

if (val % 2 == 1)
output = “Number is odd”;
else
output = “Number is even”;

Example 4:

示例 4:

if (val % 2 == 1){
output = “Number is odd”;
} else {
output = “Number is even”;
}

Similar question:

类似问题:

Why is it considered a bad practice to omit curly braces?

为什么省略花括号被认为是一种不好的做法?

采纳答案by Jon Skeet

For cases like this, there's also the conditional operator:

对于这种情况,还有条件运算符:

output = (val % 2 == 1) ? "Number is odd" : "Number is even";

If you're definitely going to use an "if" I'd use version 2 or version 4, depending on the rest of your bracing style. (At work I use 4; for personal projects I use 2.) The main thing is that there are braces even around single statements.

如果您肯定要使用“if”,我会使用第 2 版或第 4 版,具体取决于您的支撑样式的其余部分。(在工作中我使用 4;对于个人项目我使用 2。)主要的是即使在单个语句周围也有大括号。

BTW, for testing parity it's slightly quicker to use:

顺便说一句,为了测试奇偶校验,使用速度稍快:

if ((val & 1) == 1)

回答by Grzenio

I use version 2.

我使用版本 2。

回答by RuudKok

I prefer #2. Easy readability.

我更喜欢#2。易于阅读。

回答by Bryan Marble

Version 2. I always include the brackets because if you ever need to put more than one line under the conditional you won't have to worry about putting the brackets in at a later date. That, and it makes sure that ALL of your if statements have the same structure which helps when you're scanning code for a certain if statement.

版本 2。我总是包含括号,因为如果您需要在条件下放置多于一行,您将不必担心以后将括号放入。那,它确保您的所有 if 语句都具有相同的结构,这在您扫描某个 if 语句的代码时会有所帮助。

回答by Khadaji

I use version 2.

我使用版本 2。

One reason to use curly braces becomes more clear if you don't have an else.

如果没有 else,使用花括号的一个原因就会变得更加清晰。

if(SomeCondition)
{
  DoSomething();
}

If you then need to add another line of code, you are less likely to have an issue:

如果您随后需要添加另一行代码,则不太可能出现问题:

if(SomeCondition)
{ 
  DoSomething();
  DoSomethingElse();
}

Without the braces you mighthave done this:

如果没有大括号,您可能会这样做:

if(SomeCondition)
   DoSomething();
   DoSomethingElse();

回答by Bryan Rowe

I agree with the ternary operator. Very under utilized in code that I come across, and I think it is much easier and nicer to read than all the extra brackets and indents it takes to write out an if/else statement.

我同意三元运算符。在我遇到的代码中使用率很低,我认为它比写出 if/else 语句所需的所有额外括号和缩进更容易和更好阅读。

回答by Jacob Adams

I personally prefer 3. The extra curly braces just add too much unnecessary visual noise and whitespace.

我个人更喜欢 3。额外的花括号只会增加太多不必要的视觉噪音和空白。

I can somewhat see the reasoning for 2/4 to reduce bugs, but I have personally never had a bug because thinking extra lines were inside an if statement. I do use C# and visual studio so my code always stays pretty well formatted. This could however be a problem if I was a more notepad style programmer.

我在某种程度上可以看出 2/4 减少错误的原因,但我个人从未遇到过错误,因为认为额外的行在 if 语句中。我确实使用 C# 和 Visual Studio,因此我的代码始终保持良好的格式。然而,如果我是一个更记事本风格的程序员,这可能是一个问题。

回答by Todd Friedlich

I prefer 4 myself, but I think 2 is definitely good too.

我自己更喜欢 4,但我认为 2 也绝对不错。

回答by Kev

Example 2 is without a doubt the least error prone approach. Please see this answer I gave to a similar questionfor the reason why:

毫无疑问,示例 2 是最不容易出错的方法。请参阅我对类似问题的回答,原因如下:

What is the prefered style for single decision and action statements?

单一决策和行动陈述的首选风格是什么?

Although the Visual Studio default for brace usage is to put braces on a newline (my preferred method), the Framework Design Guidelinesbook (first edition) by Krzysztof Cwalina and Brad Abrams propose a different convention, example 4, placing the opening brace at the end of a preceding ifstatement (Page 274). They also state "Avoidomitting braces, even if the language allows it".

尽管 Visual Studio 默认的大括号用法是将大括号放在换行符上(我的首选方法),但Krzysztof Cwalina 和 Brad Abrams的《框架设计指南》一书(第一版)提出了一个不同的约定,例如 4,将左大括号放在前一个if语句的结尾(第 274 页)。他们还声明“避免省略大括号,即使语言允许”。

Not having the second editionat hand, I couldn't say if these conventions have changed or not.

手头没有第二版,我不能说这些约定是否已经改变。

回答by Kevin

I would use them in the following order: 1) the Ternary operator 2) example 3, but indented properly 3) either 2 or 4, they are basically the same. I would go with whatever the general styl was where I worked.

我会按以下顺序使用它们:1) 三元运算符 2) 示例 3,但缩进正确 3) 2 或 4,它们基本相同。我会选择我工作的任何通用风格。

I agree with what jake said about omitting the unnecessary curly braces. I have never caused or seen a bug caused by new code being added and someone thinking they were part of an if statement but they weren't because of the lack of curly braces. If someone ever did do that, I would ridicule them mercilessly.

我同意Hyman所说的省略不必要的花括号。我从未导致或看到由添加新代码引起的错误,有人认为它们是 if 语句的一部分,但不是因为缺少大括号。如果有人这样做,我会毫不留情地嘲笑他们。

You'd have to torture me to get me to use number 1.

你必须折磨我才能让我使用1号。