Logical, conditional, and null operators

C# allows you to combine the aforementioned operators with OR (||), AND (&&), or XOR (^). These are applied to both operands in an expression.

The following table lists the logical, conditional, and null operators:

Expression Description and example
Logical OR (|) This operator computes both operands and returns false if both are false.
Logical AND (&) This operator can be used in two forms: Unary address operator or Binary logical operator. When used as a Unary address operator, it returns the address of the operand. If used as a Binary, it evaluates both operands and returns true if both operands are true; otherwise, it will return false.
Conditional AND (&&) This conditional operator is used when two bool operands need to be evaluated. When applied, both operands are computed and returns true if both operands are true. If the first operand returns false, the conditional operator doesn't evaluate the other operator. This is also known as the short-circuiting logical AND operator.
Conditional OR (||) This is also known as the short-circuiting logical OR operator. The conditional OR operator evaluates both bool operands and returns true if either of them is true. If the first operand returns true, it won't evaluate the second operator.
Logical XOR (^) This operator is evaluated as a bitwise exclusive OR for integral types and logical exclusive and OR for bool types. When applied, it computes both operands and returns true if one of the operands is true; otherwise, it returns false.
Null coalescing (??) The null coalescing operator computes both operands and returns the operand that is not null. It's used like so: int y = x ?? 1;. In this scenario, if x is null, y is assigned a value of 1; otherwise, y is assigned a value of x.
Ternary operator (?:) The conditional operator is also known as the Ternary operator and evaluates a Boolean expression. The condition is ? true value : false value. If the condition is true, the operator returns true value, but if the condition is false, the operator returns false value. Ternary operators support nested expressions or operators, which is also known as being right-associative.

 

The following code will allow us to understand each of these statements in detail. Initially, we will define the required variables and methods and then proceed with each statement. The following code is available on GitHub. The link is provided in the Technical requirements section:

int firstvalue = 5; 
int secondvalue = 6;
int? nullvalue = null;
private bool SecondOperand(bool result)
{
Console.WriteLine("SecondOperand computed");
return result;
}
private bool FirstOperand(bool result)
{
Console.WriteLine("FirstOperand computed");
return result;
}

In the following code block, logical OR (|) shows usage of the | operator. We have two Boolean expressions in the following code block that are evaluated at runtime and return either true or false. This operator always returns true except when both operands return false:

//LOGICAL OR (|)
Console.WriteLine((firstvalue > secondvalue) | (firstvalue < secondvalue));
// output : true
Console.WriteLine((firstvalue < secondvalue) | (firstvalue < secondvalue));
// output : true
Console.WriteLine((firstvalue < secondvalue) | (firstvalue > secondvalue));
// output : true
Console.WriteLine((firstvalue > secondvalue) | (firstvalue > secondvalue));
// output : false

In the following code block, logical AND shows how the & operator can be used. Logical AND evaluates both operands and returns true if both operands are evaluated as true; otherwise, it returns false:

//LOGICAL AND (&)
Console.WriteLine(FirstOperand(true) & SecondOperand(true));
// output : FirstOperand computed, SecondOperand computed, true
Console.WriteLine(FirstOperand(false) & SecondOperand(true));
// output : FirstOperand computed, SecondOperand computed, false
Console.WriteLine(FirstOperand(true) & SecondOperand(false));
// output : FirstOperand computed, SecondOperand computed, false
Console.WriteLine(FirstOperand(false) & SecondOperand(false));
// output : FirstOperand computed, SecondOperand computed, false

In the following code clock, the conditional AND (&&) illustrates the && operator. This operator evaluates the first operator and, if it is true, it evaluates the second operator. Otherwise, it returns false:

//CONDITIONAL AND (&&)
Console.WriteLine(FirstOperand(true) && SecondOperand(true));
// output = FirstOperand computed, SecondOperand computed, true
Console.WriteLine(FirstOperand(false) && SecondOperand(true));
// output = FirstOperand computed, false
Console.WriteLine(FirstOperand(true) && SecondOperand(false));
// output = FirstOperand computed, false
Console.WriteLine(FirstOperand(false) && SecondOperand(false));
// output = FirstOperand computed, false

In the following code block, the conditional OR (||) illustrates the || operator. This operator returns true if any of the operands is true; otherwise, it returns false:

//CONDITIONAL OR (||)
Console.WriteLine(FirstOperand(true) || SecondOperand(true));
// output = FirstOperand computed, true
Console.WriteLine(FirstOperand(false) || SecondOperand(true));
// output = FirstOperand computed, SecondOperand computed, true
Console.WriteLine(FirstOperand(true) || SecondOperand(false));
// output = FirstOperand computed, true
Console.WriteLine(FirstOperand(false) || SecondOperand(false));
// output = FirstOperand computed, SecondOperand computed, false

In the following code, the logical XOR (^) explains the ^ operator on bool operands. This returns true if one of the operands is true. This is similar to the logical OR operator:

//LOGICAL XOR (^)
Console.WriteLine(FirstOperand(true) ^ SecondOperand(true));
// output = FirstOperand computed, SecondOperand computed, false
Console.WriteLine(FirstOperand(false) ^ SecondOperand(true));
// output = FirstOperand computed, SecondOperand computed,true
Console.WriteLine(FirstOperand(true) ^ SecondOperand(false));
// output = FirstOperand computed, SecondOperand computed,true
Console.WriteLine(FirstOperand(false) ^ SecondOperand(false));
// output = FirstOperand computed, SecondOperand computed,false

Here, we will look at null coalescing and the ternary operator. The null coalescing operator, ??, is used to check if an operand is null before returning its value. It returns the value if the first operand is not null; otherwise, it returns the second operand. This can be used in a nested form as well.

The ternary operator, (?:), is used to evaluate an expression. If it is true, then it returns true-value; otherwise, it returns false-value:

//Null Coalescing (??)
Console.WriteLine(nullvalue ?? firstvalue);// output : 5

//Ternary Operator (? :)
Console.WriteLine((firstvalue > secondvalue) ? firstvalue : secondvalue);// output : 6
Console.WriteLine((firstvalue < secondvalue) ? firstvalue : secondvalue);// output : 5
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.71