CJ7
CJ7

Reputation: 23275

c#: can you use a boolean predicate as the parameter to an if statement?

In C#, can you use a boolean predicate on its own as the parameter for an if statement?

eg:

string str = "HELLO";
if (str.Equals("HELLO"))
{
    Console.WriteLine("HELLO");
}

Will this code output "HELLO", or does it need to be:

string str = "HELLO";
if (str.Equals("HELLO") == true)
{
    Console.WriteLine("HELLO");
}

If there is anything else wrong with the above code segments, please point it out.

EDIT: double equals as per answers

Upvotes: 0

Views: 1185

Answers (6)

NibblyPig
NibblyPig

Reputation: 52942

The code you have is correct, the if statement checks to see if the statement within the brackets evaluates to true.

Remember that comparisons in c# use == not = , so it should be if (x == true)

This can be used in other situations like:

bool isACat = true;

if (isACat)
{
}

It is good practice to put is or a similar identifier (Is/Has/Should/Will/Do/Must...) before boolean variable names, as it makes it easy to read when you are not using == true.

Upvotes: 2

gvaish
gvaish

Reputation: 9404

There's nothing wrong.

Just two different ways of picking up the same spoon to feed yourself ;)

Upvotes: 0

AakashM
AakashM

Reputation: 63338

can you use a boolean predicate on its own as the parameter for an if statement

Strictly speaking, the answer to this is 'no', because a predicate is a function (in the mathematical sense). What you can use for the conditional expression in an if is any boolean expression, including (as here) the result of the invocation of a predicate on a value.

To expand, the 'predicate' here is 'equals HELLO'. It is a function (in the mathematical sense) that operates on values of type string, and returns boolean values. Once you have obtained a boolean value (by applying this function to a particular string, str), you do not need to explicitly compare it to true: you can just use it.

As you will see from others' answers, code in which expressions of boolean type are explicitly compared to boolean literals will often cause code-style pain in the reader :) (My 'favourite' peeve is <boolean expression> ? true : false).

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

The second version won't even compile, since you need ==, not =.

Seeing code like if (foo == true) makes bile rise up into my mouth.

Upvotes: 3

cjk
cjk

Reputation: 46425

Yes you can.

(Your second example needs 2 = (i.e. ==) to be correct).

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500625

Well, the latter snippet won't even compile because it's trying to assign true to str.Equals("Hello") (you've got a single = instead of ==) but either:

if (str.Equals("HELLO"))

or

if (str.Equals("HELLO") == true)

will work. The latter is pretty strongly discouraged though - it's pretty ugly.

Bear in mind that string overloads ==, so you can actually write:

if (str == "HELLO")

which is somewhat simpler.

Upvotes: 4

Related Questions