C# Operators

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:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Unary Operators
  7. Ternary Operator
  8. Miscellaneous Operators

Let’s explore each one with examples.

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

OperatorNameExampleResult
+Additiona + bSum of a and b
-Subtractiona - bDifference of a and b
*Multiplicationa * bProduct of a and b
/Divisiona / bQuotient of a and b
%Modulusa % bRemainder 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.

OperatorExampleEquivalent To
=a = bAssign b to a
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = 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.

OperatorNameExampleResult
==Equal toa == bTrue/False
!=Not equal toa != bTrue/False
>Greater thana > bTrue/False
<Less thana < bTrue/False
>=Greater or equal toa >= bTrue/False
<=Less or equal toa <= bTrue/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.

OperatorNameExampleDescription
&&ANDa > b && a < cTrue 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.

OperatorNameExample
&ANDa & b
``OR
^XORa ^ b
~NOT~a
<<Left Shifta << 2
>>Right Shifta >> 2

6. Unary Operators

Unary operators perform operations on a single operand.

OperatorNameExample
+Unary plus+a
-Unary minus-a
++Incrementa++ or ++a
--Decrementa-- 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

  1. Operators are fundamental for performing computations and logical operations.
  2. Learn the difference between arithmetic, logical, and comparison operators.
  3. 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.

Leave a Comment