Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using if (!bool) vs if (bool == false) in C# [closed]

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.

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

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

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

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

Thanks!

like image 345
mweber Avatar asked Sep 23 '11 23:09

mweber


People also ask

How do you check if a boolean is true or false?

To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean. Copied!

Is boolean 0 True or false?

Boolean Variables and Data Type ( or lack thereof in C ) C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

Is bool and boolean are same?

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra.

Is bool false?

FALSE are not boolean , they are Boolean . They are static instances of the two Boolean wrapper objects that correspond to the boolean values true and false . Boolean is similar to an enum . The TRUE and FALSE instances are the instances returned by Boolean.


3 Answers

The normal convention is

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)
{

}

Which is equivalent to

if (myBool.HasValue && myBool.Value)
like image 127
aquinas Avatar answered Oct 01 '22 02:10

aquinas


I don't know of any language for which the latter is preferred. Use the former.

Warning!

There's a reason for this!

This indeed does what you expect, in most languages:

if (x == false)
    ...

But in e.g. C++, because true is just a synonym for 1 (so 2 isn't true or false), this doesn't work:

if (x != true)
    ...

although it's fine in C#.

In fact, it can also get tricky in .NET -- you can trick a boolean to take an integer value, and mess it up with bitwise arithmetic (e.g. a & b can be false when a is 1 and b is 2, even though both are "true").

In general, just use the former instead of worrying about boolean literals.

like image 24
user541686 Avatar answered Oct 01 '22 00:10

user541686


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

This is the preferred version, as since you already have a bool variable that contains a true or false, there is no reason to do an additional evaluation in the if statement.


Update

Based on what aquinas has stated, this format is good to use unless you do have a nullable boolean (ex: bool? myBool). If this is the case, use the former:

bool? myBool
if (myBool == false)
{
  // Do stuff here...
}
like image 3
5StringRyan Avatar answered Oct 01 '22 01:10

5StringRyan