gpt4 book ai didi

c# - 单语句条件 - 为什么该模式不用于其他代码块?

转载 作者:太空狗 更新时间:2023-10-29 20:18:23 25 4
gpt4 key购买 nike

只是想看看是否有人可以解释为什么安德斯认为这是有效的...

if(...)
//single statement
else
///single statement

但这不是...

try
//single statement
catch
//single statement

最佳答案

引自Framework Design Guidelines在关于“通用样式约定”的部分中,这是关于大括号的:

AVOID omitting braces, even if the language allows it. Braces should not be considered optional. Even for single statement blocks, you should use braces. This increase code readability and maintainability.

There are very limited cases when omitting braces might be acceptable, such as when adding a new statement after an existing singöe-line statement is either impossible or extremely rare. For example, it is meaningless to add a statement after a throw statement:

if(someExpression) throw new
ArgumentOutOfRangeExcetion(...);

Another exception to the rule is braces in case statements. These braces can be omitted as the caseand breakstatements indicate the begining and the start of the block.

Anders 认为是主观的和有争议的,这是建议。

您可能还想查看 coding convention over at msdn. 中关于支撑的部分

关于c# - 单语句条件 - 为什么该模式不用于其他代码块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4943408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com