Comparison operators in C# are used to compare two values and return a boolean result (true or false) based on the comparison. These operators are essential for decision-making and flow control in programs. Below is a detailed explanation of each comparison operator with examples.
1. Equal To (==)
The == operator checks if two operands are equal. If they are equal, it returns true; otherwise, it returns false.
//syntax
bool result = operand1 == operand2;
//code
int a = 10;
int b = 20;
bool isEqual = (a == b); // isEqual is false
2. Not Equal To (!=)
The != operator checks if two operands are not equal. If they are not equal, it returns true; otherwise, it returns false.
//syntax
bool result = operand1 != operand2;
//Code
int a = 10;
int b = 20;
bool isNotEqual = (a != b); // isNotEqual is true
3. Greater Than (>)
The > operator checks if the left operand is greater than the right operand. If it is, it returns true; otherwise, it returns false.
//syntax
bool result = operand1 > operand2;
//Code
int a = 10;
int b = 20;
bool isGreater = (a > b); // isGreater is false
4. Less Than (<)
The < operator checks if the left operand is less than the right operand. If it is, it returns true; otherwise, it returns false.
//syntax
bool result = operand1 < operand2;
//Code
int a = 10;
int b = 20;
bool isLess = (a < b); // isLess is true
5. Greater Than or Equal To (>=)
The >= operator checks if the left operand is greater than or equal to the right operand. If it is, it returns true; otherwise, it returns false.
//syntax
bool result = operand1 >= operand2;
//
int a = 10;
int b = 10;
bool isGreaterOrEqual = (a >= b); // isGreaterOrEqual is true
6. Less Than or Equal To (<=)
The <= operator checks if the left operand is less than or equal to the right operand. If it is, it returns true; otherwise, it returns false.
// syntax
bool result = operand1 <= operand2;
//code
int a = 10;
int b = 20;
bool isLessOrEqual = (a <= b); // isLessOrEqual is true
Example
Example 1: Checking Age Eligibility
int age = 18;
bool isEligibleToVote = (age >= 18);
if (isEligibleToVote)
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
Example 2: Comparing Strings
string str1 = "hello";
string str2 = "world";
bool areStringsEqual = (str1 == str2); // areStringsEqual is false
bool areStringsNotEqual = (str1 != str2); // areStringsNotEqual is true
Console.WriteLine($"Are strings equal? {areStringsEqual}");
Console.WriteLine($"Are strings not equal? {areStringsNotEqual}");
Example 3: Checking if a Number is Within a Range
int number = 15;
bool isInRange = (number >= 10 && number <= 20);
if (isInRange)
{
Console.WriteLine("The number is within the range.");
}
else
{
Console.WriteLine("The number is out of the range.");
}
Summary
Operator | Name | Example |
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Comparison operators in C# are used to compare two values and return a boolean result (true or false) based on the comparison. These operators are essential for decision-making and flow control in programs. Below is a detailed explanation of each comparison operator with examples.
1. Equal To (==)
The == operator checks if two operands are equal. If they are equal, it returns true; otherwise, it returns false.
//syntax
bool result = operand1 == operand2; //code int a = 10; int b = 20; bool isEqual = (a == b); // isEqual is false
2. Not Equal To (!=)
The != operator checks if two operands are not equal. If they are not equal, it returns true; otherwise, it returns false.
//syntax bool result = operand1 != operand2; //Code int a = 10; int b = 20; bool isNotEqual = (a != b); // isNotEqual is true
3. Greater Than (>)
The > operator checks if the left operand is greater than the right operand. If it is, it returns true; otherwise, it returns false.
//syntax bool result = operand1 > operand2; //Code int a = 10; int b = 20; bool isGreater = (a > b); // isGreater is false
4. Less Than (<)
The < operator checks if the left operand is less than the right operand. If it is, it returns true; otherwise, it returns false.
//syntax bool result = operand1 < operand2; //Code int a = 10; int b = 20; bool isLess = (a < b); // isLess is true
5. Greater Than or Equal To (>=)
The >= operator checks if the left operand is greater than or equal to the right operand. If it is, it returns true; otherwise, it returns false.
//syntax bool result = operand1 >= operand2; // int a = 10; int b = 10; bool isGreaterOrEqual = (a >= b); // isGreaterOrEqual is true
6. Less Than or Equal To (<=)
The <= operator checks if the left operand is less than or equal to the right operand. If it is, it returns true; otherwise, it returns false.
// syntax bool result = operand1 <= operand2; //code int a = 10; int b = 20; bool isLessOrEqual = (a <= b); // isLessOrEqual is true
Example
Example 1: Checking Age Eligibility
int age = 18; bool isEligibleToVote = (age >= 18); if (isEligibleToVote) { Console.WriteLine("You are eligible to vote."); } else { Console.WriteLine("You are not eligible to vote."); }
Example 2: Comparing Strings
string str1 = "hello"; string str2 = "world"; bool areStringsEqual = (str1 == str2); // areStringsEqual is false bool areStringsNotEqual = (str1 != str2); // areStringsNotEqual is true Console.WriteLine($"Are strings equal? {areStringsEqual}"); Console.WriteLine($"Are strings not equal? {areStringsNotEqual}");
Example 3: Checking if a Number is Within a Range
int number = 15; bool isInRange = (number >= 10 && number <= 20); if (isInRange) { Console.WriteLine("The number is within the range."); } else { Console.WriteLine("The number is out of the range."); }
Summary
Operator | Name | Example |
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved