Hey! If you love C# and building C# apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Operators in C#

Unlock the full potential of C# with our comprehensive guide to operators! Learn how to use arithmetic, comparison, logical, and assignment operators to write more efficient and expressive code. Boost your coding skills today!


Updated October 18, 2023

In C#, operators are used to perform operations on values and variables. There are several types of operators available in C#, including:

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division. Here are the arithmetic operators available in C#:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modulus)

Here’s an example of using these operators:

int x = 5;
int y = 3;
int z = x + y * 2;

In this example, x is set to 5, y is set to 3, and z is set to the result of adding x and multiplying y by 2.

Comparison Operators

Comparison operators are used to compare values and return a boolean value indicating whether the comparison is true or false. Here are the comparison operators available in C#:

  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

Here’s an example of using these operators:

int age = 25;
bool isAdult = age >= 18;

In this example, age is set to 25, and isAdult is set to true because 25 is greater than or equal to 18.

Logical Operators

Logical operators are used to combine comparison operators and perform more complex operations. Here are the logical operators available in C#:

  • && (and)
  • || (or)
  • ! (not)

Here’s an example of using these operators:

bool isAdult = age >= 18;
bool isStudent = age < 25;
bool isEligible = isAdult && isStudent;

In this example, isAdult, isStudent, and isEligible are all booleans that are set based on the values of age. The && operator is used to combine the two conditions, so isEligible is only true if both isAdult and isStudent are true.

Assignment Operators

Assignment operators are used to assign a value to a variable. Here are the assignment operators available in C#:

  • = (equal to)
  • -= ( subtract)
  • += (add)
  • *=` (multiply)
  • /= (divide)

Here’s an example of using these operators:

int x = 5;
x += 2;

In this example, x is set to 5, and then the += operator is used to add 2 to x, so x now equals 7.

Other Operators

C# also includes several other operators that can be useful in your coding projects. Here are a few examples:

  • ++ (increment)
  • -- (decrement)
  • ++, -- (increment or decrement by one)
  • += (add and assign)
  • -= (subtract and assign)
  • *= (multiply and assign)
  • /= (divide and assign)

Here’s an example of using these operators:

int x = 5;
x++; // x is now 6
x -= 2; // x is now 3
x *= 2; // x is now 6
x /= 2; // x is now 3

In this example, x is set to 5, and then the ++, -, *=, and /= operators are used to increment x by one, subtract 2 from x, multiply x by 2, and divide x by 2.

Conclusion

In this article, we’ve covered the different types of operators available in C#. These operators can be used to perform a wide range of operations, from simple arithmetic to complex logical operations. By understanding how to use these operators effectively, you can write more efficient and expressive code, and take your C# skills to the next level.