C# Operators8 min read

Operators are the foundation of any programming language, the program is incomplete without the operators as we use them in all of our programs.

Operators are the symbol that performs a specific mathematical or logical operation on operands.
Example: Addition : 2 + 3, where 2 and 3 are the operands and + is the operator.

Following are the types of Operators in C#.

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Unary Operators
  • Bitwise Operators
  • Ternary or Conditional Operator
  • Miscellaneous Operators

C# Arithmetic Operators

Arithmetic Operators are used to perform mathematical operations on operands such as addition, subtraction, multiplication, division etc.

operatordescription
+addition
adds two operands. eg, a+b.
-subtraction
subtracts two operands. eg, a-b.
*multiplication
multiplies two operands. eg, a*b.
/division
divides the operand by the second. eg, a/b.
%modulo
returns the remainder when the first operand is divided by the second. For example, a%b.

Click Here for example of Arithmetic Operators operators.


C# Assignment Operators

These operators are used in a program to assign a value of the right-hand side to the left-hand side as shown below.

You can combine write in short version such as instead of writing, int a = a+5, you can write it as a += 5.

Following are the list of assignment operators used in C#.

OperatorDescription
=Simple assignment operator, Assigns values from right side operands to left side operand.
a = b + c;
+=Add AND assignment operator, It adds the right operand to the left operand and assigns the result to the left operand.
a += b
-=Subtract AND assignment operator, It subtracts the right operand from the left operand and assigns the result to the left operand.
a -= b
*=Multiply AND assignment operator, It multiplies the right operand with the left operand and assigns the result to the left operand.
a *= b
/=Divide AND assignment operator, It divides left operand with the right operand and assigns the result to the left operand.
a /= b
%=Modulus AND assignment operator, It takes modulus using two operands and assigns the result to the left operand.
a %= b
<<=Left shift AND assignment operator.
a <<= 2
>>=Right shift AND assignment operator.
a >>= 2 or a = a >> 2
&=Bitwise AND assignment operator.
a &= 2 or a = a & 2
^=Bitwise exclusive OR and assignment operator.
a ^= 2 or a = a ^ 2
|=Bitwise inclusive OR and assignment operator.
a |= 2 or a = a | 2

Click Here for example of Assignment Operators operators.


C# Relational Operators

These operators are used in a program to check the relationship between two operands and accordingly, it returns boolean values, true or false. These are used with loops and Decision-Making Statements during condition checking.

List of relational operators in C#:

OperatorNameExample
==Equal to.A == B
!=Not equal.A != B
>Greater than.A > B
<Less than.A < B
>=Greater than or equal to.A >= B
<=Less than or equal to.A <= B

Click Here for example of Relational Operators operators.


C# Logical Operators

These operators are used to compute the logical operation in a program such as &&|| and !. They are used to evaluate the condition.

The following are the C# Logical Operators. Assume X holds the value 1 and Y holds 0

OperatorDescriptionExample
&& (logical and)If both the operands are non-zero, then the condition becomes true.(X && Y) is false
|| (logical or)If any of the two operands are non-zero, then the condition becomes true.(X || Y) is true
! (logical not)Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(X && Y) is true

Click Here for example of Logical Operators operators.


C# Unary Operators

These operators are the Increment and Decrement Operators. They operate on a single operand. They are ++ and -- operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1.

  • Post-Increment or Post-Decrement:
    First, the value is used for operation and then incremented or decremented. Represented as a++ or a–.
  • Pre-Increment Pre-Decrement:
    Here First the value is incremented or decremented then used for the operation. Represented as ++a or –a.

Click Here for example of Unary Operators operators.


C# Bitwise Operators

Bitwise operators are used to perform a bit-level operation on operands. They are used in testing, setting, or shifting the actual bits in a program.

You can see the truth table below.

pqp & qp | qp ^ q~p
000001
010111
111100
100110

Below are the list of Bitwise operators:

OperatorsName of operators
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Shift left
>>Shift right

Click Here for example of Bitwise Operators operators.


C# Ternary or Conditional Operator (?:)

It is a short-hand version of the if-else statement that operates on three operands. The ternary operator (or Conditional operator) returns a true or false after checking the condition.

Syntax of ternary operator:

Explanation:

  • condition: It checks the condition for a true or false value.
  • expression1: If the condition is true expression1 is evaluated.
  • expression2: If the condition is false expression2 is evaluated.

Example of Ternary Operator in C#

Output:


Miscellaneous Operators

Apart from the above operators, there are various other operators used in C# programming and they are:

sizeof()It returns the size of a variable, constant, array, etc. sizeof is a keyword in C#.
Example:
sizeof(int); // returns 4
typeof()Returns the type of a class
Example:
typeof(StreamReader);

&
This is a pointer operator, used to point the address of a variable, or we can say that it represents the memory address of the operand to which it points.
Example:
&a;, This points to the actual address of the variable a.
*This is also a pointer variable, Indirection Operator. It returns the value of the variable that it points to.
isThis operator checks an object is of a certain type.
Example:
If(mercedes is Car) // checks if Mercedes is an object of the Car class.
asThis operator cast without raising an exception
Example:
Object ob = new StringReader("World");
StringReader r = ob as StringReader;

Operator Precedence in C#

In a program, there may be an expression that uses more than one operator, in such case operator precedence determines which is to be evaluated first and next.

Let us take an example:

The output of the above expression is 60 because the precedence of multiplication is higher than the addition. So first (5 * 10) is evaluated and then 10 is added to the result.

The following shows the precedence and associativity of C# operators:

Associativity refers to the direction of the operators to be evaluated first and next. It may be right to left or left to right.

CategoryOperatorAssociativity
Postfix() [] -> . ++ – –Left to right
Unary+ – ! ~ ++ – – (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ –Right to left
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=/td>Right to left
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Right to left
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …