如果使用(!布尔)VS如果在C#(布尔== FALSE) [英] Using if (!bool) vs if (bool == false) in C#

查看:304
本文介绍了如果使用(!布尔)VS如果在C#(布尔== FALSE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何形式的以下两种编码风格风格共识?我更好奇,如果这是诸如此类的事情,其中​​一个通常最好在良好的代码在C#中,如果此之类的事情采摘编码项目的风格时被决定。

Is there any sort of style consensus on the following two coding styles? I'm more curious if this is the sort of thing where one is generally preferred in good code in C#, or if this the sort of thing that gets decided when picking a style for a coding project.

样式1:使用!登录表示不会在条件

Style 1: Using the ! sign to indicate a not in a conditional

if (!myBool)
{
  //Do Stuff...
}

风格2:使用== false指示为虚假支票在有条件

Style 2: Using == false to indicate a check for falsehood in a conditional

if (myBool == false)
{
  //Do Stuff...
} 

谢谢!

推荐答案

正常惯例是

if (!myBool)

在一个地方,我不走这条路是可空布尔值。在这种情况下,我会做

The one place where I don't go this route is with nullable booleans. In that case I will do

if (myBool == true)
{

}

这是等同于

if (myBool.HasValue && myBool.Value)

这篇关于如果使用(!布尔)VS如果在C#(布尔== FALSE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆