C# operators are symbols used to perform operations on variables and values. This guide provides a comprehensive introduction to C# operators, covering the most common operators and their usage.
Arithmetic Operators
Arithmetic operators allow you to perform mathematical calculations in C#:
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder after division)
Example Usage
Here are some examples of using arithmetic operators:
int x = 10;
int y = 5;
int sum = x + y; // Addition
int difference = x - y; // Subtraction
int product = x * y; // Multiplication
int quotient = x / y; // Division
int remainder = x % y; // Modulus
Comparison Operators
Comparison operators are used to compare values:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Example Usage
Here are some examples of using comparison operators:
int a = 10;
int b = 5;
bool isEqual = a == b; // Equal to
bool isNotEqual = a != b; // Not equal to
bool isGreater = a > b; // Greater than
bool isLess = a < b; // Less than
bool isGreaterOrEqual = a >= b; // Greater than or equal to
bool isLessOrEqual = a <= b; // Less than or equal to
Logical Operators
Logical operators allow you to perform logical operations:
&&
: Logical AND||
: Logical OR!
: Logical NOT
Example Usage
Here are some examples of using logical operators:
bool condition1 = true;
bool condition2 = false;
bool andResult = condition1 && condition2; // Logical AND
bool orResult = condition1 || condition2; // Logical OR
bool notResult = !condition1; // Logical NOT
Conclusion
Operators are a fundamental part of C# programming. You've learned about arithmetic, comparison, and logical operators and how to use them. These operators enable you to perform a wide range of operations in your C# code.
Practice using these operators in your programs and explore more advanced operators and their applications as you continue your journey in C# programming.