Welcome to The Coding College! In this tutorial, we’ll cover C# Operators—essential tools in every C# program. Operators allow you to perform various computations and operations, such as arithmetic, comparisons, logical evaluations, and more.
This guide includes detailed explanations, examples, and code snippets to help you learn and master operators in C#.
For more programming tutorials, visit thecodingcollege.com and supercharge your coding journey!
What are Operators in C#?
Operators in C# are special symbols or keywords used to perform operations on variables and values. C# supports a variety of operators, including:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Unary Operators
- Ternary Operator
- Miscellaneous Operators
Let’s explore each one with examples.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | a + b | Sum of a and b |
- | Subtraction | a - b | Difference of a and b |
* | Multiplication | a * b | Product of a and b |
/ | Division | a / b | Quotient of a and b |
% | Modulus | a % b | Remainder of a divided by b |
Example:
int a = 10;
int b = 3;
Console.WriteLine(a + b); // Output: 13
Console.WriteLine(a - b); // Output: 7
Console.WriteLine(a * b); // Output: 30
Console.WriteLine(a / b); // Output: 3
Console.WriteLine(a % b); // Output: 1
2. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= | a = b | Assign b to a |
+= | a += b | a = a + b |
-= | a -= b | a = a - b |
*= | a *= b | a = a * b |
/= | a /= b | a = a / b |
%= | a %= b | a = a % b |
Example:
int a = 5;
a += 3; // a = a + 3
Console.WriteLine(a); // Output: 8
a *= 2; // a = a * 2
Console.WriteLine(a); // Output: 16
3. Comparison Operators
Comparison operators are used to compare two values.
Operator | Name | Example | Result |
---|---|---|---|
== | Equal to | a == b | True/False |
!= | Not equal to | a != b | True/False |
> | Greater than | a > b | True/False |
< | Less than | a < b | True/False |
>= | Greater or equal to | a >= b | True/False |
<= | Less or equal to | a <= b | True/False |
Example:
int a = 10, b = 20;
Console.WriteLine(a > b); // Output: False
Console.WriteLine(a < b); // Output: True
Console.WriteLine(a != b); // Output: True
4. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Name | Example | Description |
---|---|---|---|
&& | AND | a > b && a < c | True if both conditions are true |
` | ` | OR | |
! | NOT | !(a > b) | Reverses the result |
Example:
int a = 10, b = 20;
bool result1 = (a > 5 && b > 15); // True
bool result2 = (a > 15 || b < 10); // False
bool result3 = !(a == b); // True
Console.WriteLine(result1); // Output: True
Console.WriteLine(result2); // Output: False
Console.WriteLine(result3); // Output: True
5. Bitwise Operators
Bitwise operators work on binary representations of integers.
Operator | Name | Example |
---|---|---|
& | AND | a & b |
` | ` | OR |
^ | XOR | a ^ b |
~ | NOT | ~a |
<< | Left Shift | a << 2 |
>> | Right Shift | a >> 2 |
6. Unary Operators
Unary operators perform operations on a single operand.
Operator | Name | Example |
---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
! | NOT | !a |
7. Ternary Operator
The ternary operator is a shorthand for an if-else
statement.
Syntax:
condition ? value_if_true : value_if_false;
Example:
int a = 10, b = 20;
string result = (a > b) ? "a is greater" : "b is greater";
Console.WriteLine(result); // Output: b is greater
8. Miscellaneous Operators
sizeof
: Returns the size of a data type.typeof
: Returns the type of a class or value.is
: Checks if an object is of a specific type.as
: Casts an object to a specific type.
Key Takeaways
- Operators are fundamental for performing computations and logical operations.
- Learn the difference between arithmetic, logical, and comparison operators.
- Use the ternary operator for compact conditional statements.
Conclusion
Mastering C# operators allows you to write efficient and concise code. Experiment with the examples above to deepen your understanding.
For more coding tutorials, visit thecodingcollege.com.