- Introduction to Programming with C# 7
- C# Programming Fundamentals
- Introduction to Object-Oriented Programming
- C# Object-Oriented Programming Part 2
- Flow Control and Control Structures in C#
- C# Data Types, Variables and Casting
- C# Collection Types (Array, List, Dictionary, Hash Table)
- C# Operators: Arithmetic, Comparison, Logical and more
- Using Data in C# 7 with ADO.Net & Entity Framework
- LINQ: .NET Language Integrated Query
- Error and Exception Handling in C#
- Advanced C# Programming Topics
- Reflection in C#
- What Are ASP.Net Webforms
- Introduction to ASP.Net MVC
- Windows Application Development
- Assemblies and the Global Assembly Cache in C#
- Working with Resources Files, Culture & Regions
- Regular Expressions in C#
- Introduction to XML with C#
- Complete Guide to File Handling in C#
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 of the operators are the arithmetic and logic operators. These will be very familiar to you is you know other programming languages or mathematics.
Arithmetic
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
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
Operator | Action | Example | Result |
---|---|---|---|
++ | Increment | a=1; a++; | a = 2 |
-- | Decrement | a=1; a-- | a = 0; |
Relational
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
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 << y" | |
>>= | Right Shift | "x >>= y" is equivalent to "x = x >> y" |
Others
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.
Let's say you create a bank account class, for simplicity it will only contain a balance and holder name.
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:
testAccount1 = testAccount1 + testAccount2
or even
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. We can tell the C# compiler how to add two bank accounts together by overloading the operators.
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 a bankAccount, which contains 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 own logic within the method.
testAccount1 = testAccount1 + testAccount2;
// testAccount1.ballance = 30
// testAccount1.holdersName = "Bob Smith and Jane Doe"
Obviously, this is not how bank accounts are merged, but it gives an illustration of how operators can be overloaded for custom data types.
This post is part of the series Introduction to Programming with C#. Use the links below to advance to the next tutorial in the couse, or go back and see the previous in the tutorial series.
Thanks for the handy reference! I saved it to my bookmarks.
-Tim