
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are the logical operators in C#?
Logical operators are used with Boolean values. The following three logical operators are available in C#.
Operator | Description |
---|---|
&& | Called Logical AND operator. If both the operands are non zero then condition becomes true. |
|| | Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
Let us see an example that shows how to work with logical operators in C#. Here condition is checked for Logical AND operator.
if (a && b) { Console.WriteLine("Line 1 - Condition is true"); }
In the same way, let us see how to work with other logical operators in C#.
Example
using System; namespace Demo { class Program { static void Main(string[] args) { bool a = true; bool b = true; if (a && b) { Console.WriteLine("Line 1 - Condition is true"); } if (a || b) { Console.WriteLine("Line 2 - Condition is true"); } a = false; b = true; if (a && b) { Console.WriteLine("Line 3 - Condition is true"); } else { Console.WriteLine("Line 3 - Condition is not true"); } if (!(a && b)) { Console.WriteLine("Line 4 - Condition is true"); } Console.ReadLine(); } } }
Output
Line 1 - Condition is true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true
- Related Articles
- What are the logical operators in Java?
- What are Logical Operators in JavaScript?
- What types of logical operators are in javascript?
- Logical Operators in C++
- Python Logical Operators
- Java Logical Operators
- Perl Logical Operators
- Explain the logical operators in DBMS
- What are the differences between bitwise and logical AND operators in C/C++
- Relational and Logical Operators in C
- Logical Operators on String in Python?
- Logical Operators on String in C#
- Logical Operators on String in Java
- Java Regular expressions Logical operators
- Written version of Logical operators in C++

Advertisements