C# Operators: Arithmetic, Comparison, Logical and more

A look at the various C# operators and what they do, from arithmetic to logic this is a complete C# operator list with operator overloading.

By Tim TrottIntroduction to Programming with C# • August 11, 2008
Introduction to Programming with C#

This article is part of a series of articles. Please use the links below to navigate between the articles.

  1. Learn to Program in C# - Full Introduction to Programming Course
  2. Introdution to Programming - C# Programming Fundamentals
  3. Guide to C# Data Types, Variables and Object Casting
  4. C# Operators: Arithmetic, Comparison, Logical and more
  5. Application Flow Control and Control Structures in C#
  6. Introduction to Object Oriented Programming for Beginners
  7. Introduction to C# Object-Oriented Programming Part 2
  8. C# Collection Types (Array,List,Dictionary,HashTable and More)
  9. Error and Exception Handling in C#
  10. Events, Delegates and Extension Methods
  11. Complete Guide to File Handling in C# - Reading and Writing Files
  12. Introduction to XML and XmlDocument with C#
  13. What is LINQ? The .NET Language Integrated Query
  14. Introduction to Asynchronous Programming in C#
  15. Working with Databases using Entity Framework
  16. All About Reflection in C# To Read Metadata and Find Assemblies
  17. Debugging and Testing in C#
  18. Introduction to ASP.Net MVC Web Applications and C#
  19. Windows Application Development Using .Net and Windows Forms
  20. Assemblies and the Global Assembly Cache in C#
  21. Working with Resources Files, Culture & Regions in .Net
  22. The Ultimate Guide to Regular Expressions: Everything You Need to Know
C# Operators: Arithmetic, Comparison, Logical and more

An operator is an element applied to one or more operands in an expression or statement. These are typically arithmetic or logical operators for addition, subtraction, multiplication and division, or comparison. We will also look at changing the behaviour of operators for custom classes and structs. This process is referred to as operator overloading.

C# Operator List

The most common operators are the arithmetic and logic operators. If you know other programming languages or mathematics, these will be very familiar to you.

Arithmetic Operators

Operator Action Example Result
+ Addition z = 1 + 2 z = 3
- Subtraction z = 1 - 2 z = -1
* Multiplication z = 2 * 2 z = 4
/ Division z = 22 / 7 z = 3.142857
% Modulus z = 22 % 7 z = 1
The modulus operator (%) computes the remainder after dividing its first operand by its second.

Logic Operators

Operator Action Example Result
&& Logical AND true && false
true && true
false && false
false
true
false
|| Logical OR true || false
true || true
false || false
true
true
false
! Logical NOT true && !false true

Increment and Decrement Operators

Operator Action Example Result
++ Increment a=1;
a++;
a = 2
-- Decrement a=1;
a--
a = 0;

Relational Operators

Operator Action Example Result
== Equals x = 1;
x == 1
true
!= NOT Equals x = 1;
x != 1
false
Less than x = 1;
x <2;
true
> Greater than x = 1;
x > 0;
true
<= Less than or equal to x = 1;
x <= 0
false
>= Greater than or equal to x = 1;
x >= 5
false

Assignment Operators

Operator Action Example Result
= Assignment x = 1
+= Incremental Addition a=1;
a += 3;
a = 4;
-= Incremental Decrement a=1;
a -= 3;
a = -2;
*= Multiply by a=2;
a *= 4;
a = 8;
/= Divide by a=8;
a /= 2;
a = 4;
%= Modulus or Remainder a=8;
a %= 3;
a = 2;
&= Logical AND x &= y" is equivalent to "x = x & y"
|= Logical OR "x |= y" is equivalent to "x = x | y"
<= Left Shift "x <= y" is equivalent to "x = x
>>= Right Shift "x >>= y" is equivalent to "x = x >> y"

Other Operators


Operator Action Example Result
& Logical AND if (false & ++i == 1) false
| Logical OR true | false
false | false
true
false
^ Logical Exclusive XOR false ^ false
false ^ true
true ^ true
false
true
false
~ Bitwise Complement x = ~0x00000000 x = 0xffffffff
Left Shift 1 <1 2
>> Right Shift -1000 >> 3 -125
?? Default Value int y = x ?? -1; if x = null y = -1 else y = x
:? Conditional Operator condition ? expression if true : expression if false

C# Operator Overloading

In C#, operators can be overloaded as well as methods, a technique that allows custom data types to be manipulated in the same way as a normal data type.

You create a bank account class; for simplicity, it will only contain a balance and holder name.

C#
public class bankAccount
{
  decimal balance;
  string holdersName;
}

public class Program
{
  static void Main()
  {
    bankAccount testAccount1 = new bankAccount();
    bankAccount testAccount2 = new bankAccount();

    testAccount1.balance = 10.0;
    testAccount1.holdersName = "Bob Smith";

    testAccount2.balance = 20.0;
    testAccount2.holdersName = "Jane Doe";
  }
}

If you wanted to add testAccount2 to testAccount1, you may be tempted to try:

C#
testAccount1 = testAccount1 + testAccount2

or even

C#
testAccount1 += testAccount2

You will find that the compiler will not let you add these together as it does not know how to handle the operators for this custom type. By overloading the operators, we can tell the C# compiler how to add two bank accounts.

C#
public class bankAccount
{
  public decimal balance;
  public string holdersName;

  public static bankAccount operator +(bankAccount b1, bankAccount b2)
  {
    bankAccount temp = new bankAccount();
    temp.balance = b1.balance + b2.balance;
    temp.holdersName = b1.holdersName + " and " + b2.holdersName;
    return temp;
  }
}

This will allow the use of the + operator on the bankAccount class. It will return an account containing the sum of the two balances and the two holders' names concatenated.

All the other operators can be overloaded in the same way; all you need to do is provide your logic within the method.

C#
testAccount1 = testAccount1 + testAccount2;

// testAccount1.ballance = 30
// testAccount1.holdersName = "Bob Smith and Jane Doe"

This is not how bank accounts are merged but illustrates how operators can be overloaded for custom data types.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has only 1 comment. Why not join the discussion!

New comments for this post are currently closed.