Operators in C# can be separated in several different categories:
Arithmetic operators: They are used to perform simple mathematical operations.
Assignment operators: Allow assigning values to variables.
Comparison operators: Allow comparison of two literals and/or variables.
Logical operators: Operators that work with Boolean data types and Boolean expressions.
Binary operators: Used to perform operations on the binary representation of numerical data.
Type conversion operators: Allow conversion of data from one type to another.
Operator Categories
Below is a list of the operators, separated into categories:
Types of Operators by Number of Arguments
Operators can be separated into different types according to the number of arguments they could take:
Example of Arithmetical Operators
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | static void Main(string[] args) { int squarePerimeter = 17; double squareSide = squarePerimeter / 4.0; double squareArea = squareSide * squareSide; Console.WriteLine(squareSide); // 4.25 Console.WriteLine(squareArea); // 18.0625 int a = 5; int b = 4; Console.WriteLine(a + b); // 9 Console.WriteLine(a + (b++)); // 9 Console.WriteLine(a + b); // 10 Console.WriteLine(a + (++b)); // 11 Console.WriteLine(a + b); // 11 Console.WriteLine(14 / a); // 2 Console.WriteLine(14 % a); // 4 int one = 1; int zero = 0; // Console.WriteLine(one / zero); // DivideByZeroException double dMinusOne = -1.0; double dZero = 0.0; Console.WriteLine(dMinusOne / zero); // -Infinity Console.WriteLine(one / dZero); // Infinity Console.ReadKey(); } |
Output:
Example of Logical Operators
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | static void Main(string[] args) { bool a = true; bool b = false; Console.WriteLine(a && b); // False Console.WriteLine(a || b); // True Console.WriteLine(!b); // True Console.WriteLine(b || true); // True Console.WriteLine((5 > 7) ^ (a == b)); // False Console.ReadKey(); } |
Output:
Example of Bitwise Operators
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | static void Main(string[] args) { byte a = 3; // 0000 0011 = 3 byte b = 5; // 0000 0101 = 5 Console.WriteLine(a | b); // 0000 0111 = 7 Console.WriteLine(a & b); // 0000 0001 = 1 Console.WriteLine(a ^ b); // 0000 0110 = 6 Console.WriteLine(~a & b); // 0000 0100 = 4 Console.WriteLine(a << 1); // 0000 0110 = 6 Console.WriteLine(a << 2); // 0000 1100 = 12 Console.WriteLine(a >> 1); // 0000 0001 = 1 Console.ReadKey(); } |
Example of Comparison Operators
C# Code:
1 2 3 4 5 6 7 8 9 | int x = 10, y = 5; Console.WriteLine("x > y : " + (x > y)); // True Console.WriteLine("x < y : " + (x < y)); // False Console.WriteLine("x >= y : " + (x >= y)); // True Console.WriteLine("x <= y : " + (x <= y)); // False Console.WriteLine("x == y : " + (x == y)); // False Console.WriteLine("x != y : " + (x != y)); // True |
Example of Conditional Operator “?:”
C# Code:
1 2 3 4 5 6 7 8 9 10 11 | static void Main(string[] args) { int a = 6; int b = 4; Console.WriteLine(a > b ? "a>b" : "b<=a"); // a>b int num = a == b ? 1 : -1; // num will have value -1 Console.WriteLine(num); Console.ReadKey(); } |
Output: