Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Methods define an object's behaviors.
In this video we used three different conditional operators. &&
is called the conditional-and operator, ||
is the conditional-or operator, and !
is the logical negation operator. These are also known as boolean logic operators. When learning boolean logic it is often helpful to see the truth tables of the operations. Truth tables list all the possible values of the operands that the operators can take. The far right column of the table shows the result of doing the operation.
X | Y | X && Y |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
X | Y | X || Y |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
X | !X |
---|---|
true | false |
false | true |
Short-Circuiting
When evaluating an expression involving &&
or ||
operators, C# uses a practice called short-circuiting. This means that C# will try to do as little work as possible when evaluating a boolean expression. The code "short-circuits" differently depending on the operator being used.
For example, consider this expression:
x || y || z
If x
is true, then C# won't even consider the values of y
or z
, because when using ||
only one of the values must be true in order for the entire expression to evaluate to true. Look at the truth table for ||
to see this fact.
Likewise, when using &&
:
x && y && z
If x
is false, then C# won't look at the values of y
or z
, because when using &&
only one of the values must be false in order for the entire expression to evaluate to false. Look at the truth table for &&
to see this fact.
This is especially important to understand when the conditional expression contains more complex code. Consider this example:
x || SomeMethodThatReturnsABoolean()
If x
is true, then the method will not be run. If x
is false, then C# will continue evaluating the expression looking for the first operand that evaluates to true, and the method will be run as part of that evaluation.
In general it's best practice to keep conditional expressions as simple as possible. There are a few examples of times when short-circuiting can be very helpful. Consider the following example where I only want to do something if the names
array contains at least one item.
if(names != null && names.Length > 0)
{
// Do something with names
}
Without short-circuiting, accessing the Length
property of the names
array would throw an exception if names
was null
. But with short-circuiting, names.Length
will never get run if names
is null
so it's safe to put both these expressions together. Just remember that order is important. Boolean expressions are evaluated from left to right, with respect to parenthesis of course.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up