Behind the Scenes Look at C#: Operators - Logical Operators
(Page 5 of 8 )
C# has eight logical operators, which form logical expressions. Logical expressions are used with control structure statements to control the execution of your application's code. Logical expressions work on Boolean Arguments and produce Boolean value that will be used by the control structure statements like the if statement. C# logical operators include: the NOT operator (!), the XOR (^) or the Exclusive OR Operator, the Short Circuit And Operator (&&), the AND Operator (&), the Short Circuit OR Operator (||) and the OR Operator (|). Let's take a look at an example that use all of these operators.
using System;
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
int x = 10;
int y = 5;
bool result;
bool another;
// using the NOT Operator
Console.WriteLine("Using the ! Operator");
result = true;
Console.WriteLine("result before using ! equal to = {0}", result);
result = !result;
Console.WriteLine("result after using ! equal to = {0}", result);
Console.WriteLine();
Console.WriteLine("----------");
// using the XOR Operator
Console.WriteLine("using the ^ Operator");
result = true;
another = true;
Console.WriteLine("using the ^ with 2 true arguments");
Console.WriteLine("result ^ another equals to {0}", result ^ another);
Console.WriteLine("using the ^ with 2 false arguments");
result = false;
another = false;
Console.WriteLine("result ^ another equals to {0}", result ^ another);
Console.WriteLine("using the ^ with 1 true and another false arguments");
result = true;
another = false;
Console.WriteLine("result ^ another equals to {0}", result ^ another);
// using the && operator
Console.WriteLine();
Console.WriteLine("----------");
result = true;
if(result && ReturnTrue())
{
Console.WriteLine("using the short circuit and (&&)"
+ " the ReturnTrue() method will be called only if the"
+ " result is true");
}
Console.WriteLine();
Console.WriteLine("setting result to false");
result = false;
if(result && ReturnTrue())
{
Console.WriteLine("Do you see this line?");
}
Console.WriteLine("although that ReturnTrue() returns true it will not called" +
" because result is false and the if block will not execute");
// using the || operator
Console.WriteLine();
Console.WriteLine("---------");
Console.WriteLine("using the || operator");
Console.WriteLine("setting result to true");
result = true;
if(result || ReturnTrue())
{
Console.WriteLine("ReturnTrue() will not be called because" +
" result = true");
}
Console.WriteLine("setting result to false and use the || again");
result = false;
if(result || ReturnTrue())
{
Console.WriteLine("ReturnTrue() is called because"
+ " result = false");
}
Console.WriteLine();
Console.WriteLine("-----------");
// using the full evaluation and operator &
Console.WriteLine("setting result to true");
result = true;
Console.WriteLine("using the full evaluation and operator &");
if(result & ReturnTrue())
{
Console.WriteLine("ReturnTrue() is called if result is true or false");
}
Console.WriteLine("-----------");
Console.WriteLine("setting result to false");
result = false;
Console.WriteLine("using the full evaluation and operator &");
if(result & ReturnTrue())
{
Console.WriteLine("ReturnTrue() is called if result is true or false");
}
Console.ReadLine();
}
static bool ReturnTrue()
{
Console.WriteLine("Inside the ReturnTrue method now");
return true;
}
}
}
Yes it's a lot of code, but it's simple. The result that will be displayed in the console window will look like this:

What happened? We have used some integer variables and some other Boolean variables in order to test the functionality that these operators offer us. The NOT operator just inverts the Boolean value of the variable; we have tested it with the result variable.
The Exclusive OR Operator (^) as its name implies, is exclusive, which means that it will evaluate to true if only one of the two arguments is true. If both arguments are true it will evaluate to false, and if both arguments are false it will evaluate to false too.
The Short Circuit And Operator (&&) is familiar to C++ developers (as is the Short Circuit Or ||). The Short Circuit And operator evaluates the second argument only if the first argument is true. If the first argument is false (this means that the expression will be evaluated to false, because with the And Operator both arguments must evaluates to true) it will not evaluate the second expression. The Short Circuit Or Operator (||) is similar, it will short circuit when the first argument evaluates to true. In other words, there's no need to evaluate the second expression, because with the OR Operator it will evaluate to true when one expression evaluates to true. And the above example illustrate this mechanism.
The And (&) and Or (|) Operators are called full evaluation operators because they evaluate both of the expressions whether the first expression evaluates to true or false. Sometimes you need this behavior. In the above example, if(result & ReturnTrue()) you need the call to the method ReturnTrue() whether the first expression evaluated to true or false.
Next: Assignment Operators >>
More C# Articles
More By Michael Youssef