C# Operators Tutorial
In this tutorial lesson we learn the different operators, with explanations and examples, available in C#. This tutorial covers arithmetic, comparison, logical, assignment and bitwise operators.
What are operators
An operator is a symbol that has a special meaning in C#. They indicate the types of operations that can be done within our code, such as mathematical or logical manipulations.
C# provides us with the following operators:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Other Operators
Throughout the tutorial series we’ll be using many of these and their uses will become clearer.
Arithmetic operators
We use arithmetic operators in computations where we’re working with numbers.
The following table shows all the arithmetic operators supported by C#:
Name | Operator | Example | Same as |
---|---|---|---|
Add | + | a + b | |
Subtract | - | a - b | |
Multiply | * | a * b | |
Divide | / | a / b | |
Modulus | % | a % b | |
Increment | ++ | a++ | a + 1 |
Decrement | -- | b-- | b - 1 |
using System;
namespace Operators
{
class Program
{
static void Main(string[] args)
{
// Add
Console.WriteLine("10 + 5 = {0}", 10 + 5);
// Subtract
Console.WriteLine("15 - 3 = {0}", 15 - 3);
// Multiply
Console.WriteLine("2 * 2 = {0}", 2 * 2);
// Divide
Console.WriteLine("27 / 25 = {0}", 27 / 25);
// Modulus (remainder of division)
// 27 divided by 25 is 1, with 2 remaining
Console.WriteLine("27 % 25 = {0}", 27 % 25);
// Increment
int a = 1;
a++;
Console.WriteLine("1++ = {0}", a);
// Decrement
a = 5;
a--;
Console.WriteLine("5-- = {0}", a);
Console.ReadLine();
}
}
}
Comparison operators
We use comparison operators in to compare values.
The following table shows all the comparison operators supported by C#:
Name | Operator | Example | Result |
---|---|---|---|
Equal | == | 1 == 1 | true |
Not Equal | != | 1 != 1 | false |
Greater than | > | 2 > 1 | true |
Greater than or equal to | >= | 1 >= 1 | true |
Less than | < | 2 < 1 | false |
Less than or equal to | <= | 1 <= 2 | true |
using System;
namespace Operators
{
class Program
{
static void Main(string[] args)
{
// Equal
Console.WriteLine("1 == 1: {0}", 1 == 1);
// Not equal
Console.WriteLine("1 != 1: {0}", 1 != 1);
// Greater than
Console.WriteLine("1 > 2: {0}", 1 > 2);
// Greater than or equal to
Console.WriteLine("1 >= 1: {0}", 1 >= 1);
// Less than
Console.WriteLine("2 < 1: {0}", 2 < 1);
// Less than or equal to
Console.WriteLine("1 <= 1: {0}", 1 <= 1);
Console.ReadLine();
}
}
}
Logical operators
Logical operations are part of boolean algebra, which is often taught to computer science students.
In boolean algebra, the value can only be true or false, also denoted 1 and 0 respectively.
The main operations of boolean algebra are conjunction (AND), disjunction (OR), and negation (NOT).
Operator | Description | Example |
---|---|---|
&& | AND | 2 > 1 && 2 < 3 |
|| | OR | 2 != 1 || 1 == 1 |
! | NOT | !true |
using System;
namespace Operators
{
class Program
{
static void Main(string[] args)
{
// AND
// one thing AND another
Console.WriteLine("AND: {0}", 2 > 1 && 2 < 3);
// OR
// one thing OR another
Console.WriteLine("OR: {0}", 2 != 1 || 3 == 1);
// NOT
// not a thing
Console.WriteLine("NOT: {0}", !true);
Console.ReadLine();
}
}
}
Assignment operators
Assignment operators are used to assign values
The following table shows all the assignment operators supported by C#:
Name | Operator | Example | Same as |
---|---|---|---|
Assign | = | a = 2 | |
Add & Assign | += | a += 2 | a = a + 2 |
Subtract & Assign | -= | a -= 2 | a = a - 2 |
Multiply & Assign | *= | a *= 2 | a = a* 2 |
Divide & Assign | /= | a /= 2 | a = a / 2 |
Modulus & Assign | %= | a %= 2 | a = a % 2 |
The following table shows all the bitwise assignment operators supported by C#:
Name | Operator | Example | Same as |
---|---|---|---|
Left shift & Assign | <<= | a <<= 2 | a = a << 2 |
Right shift & Assign | >>= | a >>= 2 | a = a >> 2 |
Bitwise AND & Assign | &= | a &= 2 | a = a & 2 |
Bitwise XOR & Assign | ^= | a ^= 2 | a = a ^ 2 |
Bitwise OR & Assign | |= | a |= 2 | a = a |= 2 |
using System;
namespace Operators
{
class Program
{
static void Main(string[] args)
{
// Value assignment
int a = 1;
Console.WriteLine("= {0}", a);
// Add & assign
a += 2;
Console.WriteLine("+= 2: {0}", a);
// Subtract & assign
a -= 1;
Console.WriteLine("-= 1: {0}", a);
// Multiply & assign
a *= 5;
Console.WriteLine("*= 5: {0}", a);
// Divide & assign
a /= 2;
Console.WriteLine("/= 2: {0}", a);
// Modulus & assign
a %= 2;
Console.WriteLine("%= 2: {0}", a);
// Bitwise left shift and assign
a <<= 2;
Console.WriteLine("<<= 2: {0}", a);
// Bitwise right shift and assign
a >>= 2;
Console.WriteLine(">>= 2: {0}", a);
// Bitwise AND & assign
a &= 2;
Console.WriteLine("&= 2: {0}", a);
// Bitwise XOR (exclusive or) & assign
a ^= 2;
Console.WriteLine("^= 2: {0}", a);
// Bitwise OR (inclusive or) & assign
a |= 2;
Console.WriteLine("|= 2: {0}", a);
Console.ReadLine();
}
}
}
Bitwise operators
Bitwise operators are often used in low-level programming, like working with the Windows API, or encryption, or websockets.
The following table shows all the bitwise operators supported by C#:
Operator | Description |
---|---|
& | Binary AND |
| | Binary OR |
^ | Binary XOR |
~ | Binary Complement |
<< | Binary Left Shift |
>> | Binary Right Shift |
Other operators
There are a few other operators in C# that we need to be aware of. We’ll go through them as needed throughout the tutorial series.
Operator | Description |
---|---|
sizeof() | Returns the bit size of a data type |
typeof() | Returns the type of a class |
& | Returns the address in memory of a variable |
* | Pointer to a variable |
? : | Shortened conditional expression (ternary) |
is | Determines whether a class object is of a certain type |
as | Do a cast without throwing an exception (error) if the cast fails |
Summary: Points to remember
- Operators are symbols in C# that have special meaning and allow us to perform operations and manipulations on our code.