#169 – The if Statement Must Always Include a Boolean Expression

In C++, an if statement can use a conditional expression that resolves to a numeric value.  The statement following the if statement will execute when this expression resolves to any non-zero value.  This form of the if statement is not allowed in C#.

            uint leaves = CountLeavesInBackyard();

            // Works in C++, but is not allowed in C#
            if (leaves)
            {
                RakeLikeHeck();
            }

Since the if statement must use a boolean expression in C#, we’d rewrite the code as:

            if (leaves > 0)
            {
                RakeLikeHeck();
            }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: