As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center作为指导。
                            
                        
                    
                
                                已关闭8年。
            
                    
对于那些误解了问题的人:让我们假装we're on Wikipedia
我不是在寻找“正确的方式”,而是在寻找可验证的参考来支持任何一方。

(请读到最后)

上下文

一个人在对不同话题的评论中说,他的朋友们会与我对是否

if (!condition)


要么

if (condition == false)


在C#中是首选。

尽管我确定自己知道该怎么做,但我还是找不到任何证据证明我的观点在C#编码和设计指南中均不是官方的。

问题

除了常识之外,还有什么实质性的东西可以支持任何一方?

(在广受赞誉的书中的段落或在microsoft.com托管的任何使用或规定了另一种样式的文件都会回答该问题)

最佳答案

if (!condition)if (condition)相比,if (condition == false)if (condition == true)更可取,因为前者具有可读性,且不那么冗长。

C# Coding Standards for .NET创建的Lance Hunt页13的底部部分说:


  避免评估布尔条件是否为真。


// Bad!
if (isValid == true)
{ ... }
// Good!
if (isValid)
{ ... }


但是,它绝不是官方的,也不来自Microsoft。

关于c# - 是否有任何引用/MSDN教程显示对“if(!condition)”或“if(condition == false)”的支持? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6336735/

10-17 02:24