C# Operators: Arithmetic, Comparison, Logical and moreA look at the various C# operators and what they do, from arithmetic to logic this is a complete C# operator list with operator overloading.
This article is part of a series of articles. Please use the links below to navigate between the articles.
- Learn to Program in C# - Full Introduction to Programming Course
- Introdution to Programming - C# Programming Fundamentals
- Introduction to Object Oriented Programming for Beginners
- Introduction to C# Object-Oriented Programming Part 2
- Application Flow Control and Control Structures in C#
- Guide to C# Data Types, Variables and Object Casting
- C# Collection Types (Array,List,Dictionary,HashTable and More)
- C# Operators: Arithmetic, Comparison, Logical and more
- Using Entity Framework & ADO.Net Data in C# 7
- What is LINQ? The .NET Language Integrated Query
- Error and Exception Handling in C#
- Advanced C# Programming Topics
- All About Reflection in C# To Read Metadata and Find Assemblies
- What Are ASP.Net WebForms
- Introduction to ASP.Net MVC Web Applications and C#
- Windows Application Development Using .Net and Windows Forms
- Assemblies and the Global Assembly Cache in C#
- Working with Resources Files, Culture & Regions in .Net
- The Ultimate Guide to Regular Expressions: Everything You Need to Know
- Introduction to XML and XmlDocument with C#
- Complete Guide to File Handling in C# - Reading and Writing Files
An operator is an element that is applied to one or more operands in an expression or statement. These are typically arithmetic or logical operators used 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 of the operators are the arithmetic and logic operators. These will be very familiar to you if you know other programming languages or mathematics.
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 |