What are the logical operators "and", "or", and "not" in C#?
A logical operator is a symbol or word that connects two or more expressions. This is so that the value of the produced expression is solely determined by the value of the original expressions and the operator’s meaning.
Following are the logical operators available in the C# language:
andornot
Description
The following table explains each of the logical operators in C#.
Operator Symbol | Operator Name | Example | Description |
&& | Logical And | Operand-A && Operand-B | It returns True, when both the operand-A and operand-B is True, otherwise it returns False. |
|| | Logical Or | Operand-A || Operand-B | It returns True, when either the operand-A or operand-B is True, otherwise it returns False. |
! | Logical Not | ! Operand-A | It returns the operand's logical state in reverse. |
The following figure is the visual representation of the logical operators in C#.
Code
The code below illustrates how to use these logical operators in C#.
class Logical{static void Main(){int A=10, B=20, C=30;//And Operator//Both TrueSystem.Console.WriteLine("The value of (A<B && B<C): "+ (A<B && B<C));//First operand: True Second Operand: FalseSystem.Console.WriteLine("The value of (A<B && B>C): "+ (A<B && B>C));//Both FalseSystem.Console.WriteLine("The value of (A>B && B>C): "+ (A>B && B>C));//OR operator//Both TrueSystem.Console.WriteLine("The value of (A<B || B<C): "+ (A<B || B<C));//First operand: True Second Operand: FalseSystem.Console.WriteLine("The value of (A<B || B>C): "+ (A<B || B>C));//Both FalseSystem.Console.WriteLine("The value of (A>B || B>C): "+ (A>B || B>C));//Not operatorbool D=true;//reverse of true logical stateSystem.Console.WriteLine("The value of (!D): "+ (!D));}}