31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Search...
C# Operators
3 Apr 2025 | 12 min read
In C#, operators are special symbols that are to perform operations on variables and values on operands. Operators are important concepts in any
programming language. Operators may take one or multiple operands to perform operations and produce a result. There can be perform multiple
operations, such as arithmetic, value assignment, bitwise, and logical computations.
Simple C# Operator Example:
Let us take a simple example to illustrate operators in C#.
Example
using System;
class BasicExample {
static void Main() {
int a = 10, b = 20;
int c = a + b;
[Link]("The value of a + b is " + (c)); // Addition
}
}
Compile and Run
Output:
The value of a + b is 30.
Types of Operators:
There are several types of operators that may perform different types of operations in C#. Important C# operators are as follows:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Unary Operators
7. Ternary or Conditional Operators
8. Null-Coalescing Operators
9. Operator Associativity and Precedence
Here, we will discuss these operators one by one with their types and examples.
[Link] 1/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
1. Arithmetic Operators
In C#, arithmetic operators are mainly used to perform basic mathematical operations (including addition, subtraction, multiplication, division, etc.)
on the operands.
Operator Name Symbol Description
Addition + It is commonly utilized to add two
numbers together.
Subtraction - It is commonly utilized to subtract one
number from another.
Multiplication * It is commonly utilized to multiply two
numbers.
Division / It is commonly utilized to divide one
number by another.
Modulus % It returns the remainder of division.
C# Arithmetic Operators Example:
Let us take an example to illustrate the arithmetic operators in C#.
Example
using System;
class Arithmetic {
static void Main() //main method
{
int a = 10, b = 3; //initializing variables and values
[Link]("The value of a + b is " + (a + b)); // Addition
[Link]("The value of a - b is " + (a - b)); // Subtraction
[Link]("The value of a * b is " + (a * b)); // Multiplication
[Link]("The value of a / b is " + (a / b)); // Division
[Link]("The value of a % b is " + (a % b)); // Modulus
}
}
Compile and Run
Output:
The value of a + b is 13
The value of a - b is 7
The value of a * b is 30
The value of a / b is 3
The value of a % b is 1
[Link] 2/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
2. Relational (Comparison) Operators
In C#, a relational operator is mainly utilized to compare two values and returns a boolean value (true or false).
Operator Name Symbol Description
Equal to == It checks both operands are equal to one
another. If both operands are equal, it
returns true; else return false.
Not equal to != It checks if the values of two operands are
equal or not. If the values are not equal,
the condition becomes true. If values are
equal, the condition becomes false.
Greater than > It is utilized to compare two values and
returns true if the first operand is greater
than the second. If the first operand is not
greater than the second, it returns false.
Less than < It is also utilized to compare two values
and returns true if the first operand is less
than the second operand. Otherwise, it
returns false.
Greater than or equal to >= It returns true if the first operand is greater
than or equal to the second operand.
Otherwise, it returns false.
Less than or equal to <= It returns true if the first operand is less
than or equal to the second. Otherwise, it
returns false.
C# Relational Operators Example:
Let us take an example to illustrate the relational operator's example in C#.
Example
using System;
class Relational
{
static void Main() //main method
{
int a = 15, b = 30; //initializing variables and values
[Link]("The value of a == b is " + (a == b)); // Equal to operator
[Link]("The value of a != b is " + (a != b)); // Not equal to operator
[Link]("The value of a > b is " + (a > b)); // Greater than operator
[Link]("The value of a < b is " + (a < b)); // Less than operator
[Link]("The value of a >= b is " + (a >= b)); // Greater than or equal to
[Link] 3/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
[Link]("The value of a <= b is " + (a <= b)); // Less than or equal to operator
}
}
Compile and Run
Output:
The value of a == b is False
The value of a != b is True
The value of a > b is False
The value of a < b is True
The value of a >= b is False
The value of a <= b is True
3. Logical Operators
In C#, logical operators are utilized to combine more than one condition and return a boolean result.
Operator Name Symbol Description
Logical AND && It returns true only if both the operands
are non-zero. Otherwise, it will give false if
either or both conditions given are false.
Logical OR || It returns true only if one of the conditions
is true. If both conditions are false, it
returns false only.
Logical NOT ! It inverts the Boolean value of a condition.
If a condition is true, it returns false. If a
operand value is false, it returns true only.
C# Logical Operators Example:
Let us take an example to illustrate the logical operator in C#.
Example
using System;
class Logical
{
static void Main() //main method
{
bool x = true, y = false; //initializing Boolean variable and value
[Link]("The value of x && y is " + (x && y)); // Logical AND
[Link]("The value of x || y is " + (x || y)); // Logical OR
[Link]("The value of !x is " + (!x)); // Logical NOT
}
[Link] 4/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Compile and Run
Output:
Python Java JavaScript SQL C C++ HTML CSS React Node js Spring Boot C#
The value of x && y is False
The value of x || y is True
The value of !x is False
4. Bitwise Operators
In C#, bitwise operators operate at the binary level to modify bits of a number.
Operator Name Symbol Description
Bitwise AND & If this operator exists in both operands, it
copies a bit to the result.
Bitwise OR | If it is available in any of the operands, it
copies a bit to the calculated result.
Bitwise XOR ^ If it is available in any of the operands, it
copies a bit to the calculated result.
Bitwise Complement ~ It is unary and has the effect of flipping
bits. It reverses the binary values 1 to 0
and 0 to 1
Left Shift << It shifts all bits in a number a specified
number of positions to the left and fills the
empty position with 0. It multiplies the
number by 2n.
Right Shift >> It shifts all bits of a number to the right by
a defined number of positions. In order to
positive numbers, empty bits are filled
with 0. In order to negative numbers, the
behaviour depends on the
implementation.
C# Bitwise Operators Example:
Let us take an example to illustrate the bitwise operators in C#.
Example
using System;
class Bitwise
{
[Link] 5/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
static void Main() //main function
{
int a = 10, b = 15; //initializing variables and values
[Link]("The value of a & b is " + (a & b)); // Bitwise AND
[Link]("The value of a | b is " + (a | b)); // Bitwise OR
[Link]("The value of a ^ b is " + (a ^ b)); // Bitwise XOR
[Link]("The value of ~a is " + (~a)); // Bitwise Complement
[Link]("The value of a << b is " + (a << 1)); //Bitwise Left Shift
[Link]("The value of a >> b is " + (a >> 1)); //Bitwise Right Shift
}
}
Compile and Run
Output:
The value of a & b is 10
The value of a | b is 15
The value of a ^ b is 5
The value of ~a is -11
The value of a << b is 20
The value of a >> b is 5
5. Assignment Operators
In C#, the assignment operators are mainly used to assign values to a variable and perform operations simultaneously. The most commonly used
operator is the assignment operator (=). It allows us to modify the value stored in the variable.
Operator Name Symbol Description
Assign = It is commonly utilized to assign the value
of the right operand to the left operand.
Add and assign += It allows us to add the right operand to
the left operand and assign the result
value to the left operand.
Subtract and assign -= It allows us to subtract the right operand
from the left operand and assign the result
value to the left operand.
Multiply and assign *= It allows us to multiply the left operand by
the right operand and assign the result
number to the left operand.
Divide and assign /= It allows us to divide the left operand by
the right operand and assign the result
value to the left operand.
Modulus and assign %= It allows us to compute the remainder
when the left operand is divided by the
[Link] 6/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
right operand and assign the result
number to the left operand.
C# Assignment Operators Example:
Let us take an example to illustrate the assignment operator in C#.
Example
using System;
class Program
{
static void Main() //main function
{
int a = 10, b = 6; //initializing variables and values
[Link]("The value of a is " + (a)); //using Assignment Operator
[Link]("The value of a+=b is " + (a += b)); //using Add and Assignment Operator
[Link]("The value of a-=b is " + (a -= b)); //using Subtract and Assignment Operator
[Link]("The value of a*=b is " + (a *= b)); //using Multiply and Assignment Operator
[Link]("The value of a/=b is " + (a /= b)); //using Divide and Assignment Operator
[Link]("The value of a%=b is " + (a %= b)); //using Modulus and Assignment Operator
}
}
Compile and Run
Output:
The value of a is 10
The value of a+=b is 16
The value of a-=b is 10
The value of a*=b is 60
The value of a/=b is 10
The value of a%=b is 4
6. Unary Operators
In C#, unary operators perform operations on a single operand.
Operator Name Symbol Description
Unary Plus + It represents a positive value. It is usually
optional because numbers are positive by
[Link] 7/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
default.
Unary Minus - It neglects the value of a variable, which
transform a positive number to a negative
and vice versa.
Increment ++ It increases the value of a variable by 1
Prefix (++x): It increments the value first,
and then it is utilized in the program.
Postfix (x++): It uses the value first and
then increments it.
Decrement -- It decreases the value of a variable by 1
Prefix (--x): It decreases the value first,
and then it is utilized in the program.
Postfix (x--): It uses the value first and
then decreases it.
C# Unary Operators Example:
Let us take an example to illustrate the unary operator in C#.
Example
using System;
class Unary
{
static void Main() //main method
{
int a = 10; //initialize variable and value
[Link]("The value of +a is " + (+a)); // Unary plus
[Link]("The value of -a is " + (-a)); // Unary minus
[Link]("The value of a after a++ is " + (a++)); // Increment
[Link]("The value of a after a-- is " + (--a)); // Decrement
}
}
Compile and Run
Output:
The value of +a is 10
The value of -a is -10
The value of a after a++ is 10
The value of a after a-- is 10
[Link] 8/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
7. Ternary or Conditional Operator
In C#, the ternary operator is an alternative to if-else statements. It mainly performs the operations on three operands. It is conditional-based
operator.
Syntax:
It has the following syntax:
(Condition) ? Expression_a : Expression_b;
Here, two conditions are occurred:
If the given condition is true, Expression_a is executed and provides the appropriate result.
If the given condition is false, Expression_b is executed and provides the appropriate result.
C# Ternary or Conditional Operator Example:
Let us take an example to illustrate the ternary and conditional operator in C#.
Example
using System;
class Ternary
{
static void Main() //main method
{
int a = 15, b = 20;
int min = (a < b) ? a : b; // If a < b, min = a, otherwise min = b
[Link]("The minimum value is " + min);
}
}
Compile and Run
Output:
The minimum value is 15
8. Null Coalescing Operators
In C#, null coalescing operators provide a default value when dealing with null values.
Operator Name Symbol Description
Null Coalescing ?? It checks whether or not the left side
operand is null. It returns the left operand
if it is not null. If it is null, it returns the
[Link] 9/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
right operand. It enables us to provide
default values.
Null Coalescing Assignment ??= It sets the left operand to the right
operand only if the left operand is null.
C# Null Coalescing Operators Example:
Let us take an example to illustrate the null and coalescing operator in C#.
Example
using System;
class Program {
static void Main() {
int? num = null;
int result = num ?? 100; // If num is null, assign 100
[Link]("The result is " + result);
int? x = null;
x ??= 50; // Assigns 50 to x if it is null
[Link]("The value of x is " + x);
}
}
Compile and Run
Output:
The result is 100
The value of x is 50
9. Operator Associativity and Precedence
In C#, operator precedence is the rule which define the evaluation order of different operators in an expression. Operator precedence determines
the order of evaluation for different operators, while associativity describes the order between operators of the same precedence, telling whether to
evaluate from the left or right.
1. Operator Precedence
Operators with higher precedence are evaluated first. For example, in the expression:
Example
int result = 10 + 5 * 2;
[Link](result);
[Link] 10/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Output:
20
Here, multiplication (*) has higher precedence than addition (+), so 5 * 2 is calculated first, and then 10 is added.
2. Operator Associativity
If operators are at the same precedence level, their associativity defines evaluation order:
Left-to-Right Associativity: Operators like +, -, *, /, %, &&, ||, ==, etc., are evaluated from left to right.
Right-to-Left Associativity: Operators like = (assignment), +=, -=, ??=, and the ternary operator (?:) are evaluated from right to left.
Example of Operator Precedence and Associativity
Example
int result = 10 - 2 * 3 + 4;
[Link](result);
Output:
Since * has higher precedence than - and +, it is evaluated first.
Example of Right-to-Left Associativity
Example
int a = 5;
int b = 10;
int result = a = b = 20; // Right to Left evaluation
[Link](result);
Output:
20
Here, b = 20 is evaluated first, then a = 20, and finally result = 20.
C# Operators Multiple Choice Questions (MCQs)
1. What will be the output of the following C# code?
int a = 5, b = 10, c;
c = a++ + --b;
[Link] 11/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
[Link](c);
a. 14
b. 15
c. 16
d. 13
Show Answer Workspace
2. What will be the output of the following C# code?
int a = 10;
int b = 20;
int c = 30;
bool result = (a < b) && (b > c) || (a < c);
[Link](result);
a. True
b. False
c. Compilation Error
d. Runtime Error
Show Answer Workspace
3. What is the output of the following code?
int? x = null;
int y = x ?? 50;
[Link](y);
a. null
b. 50
c. 0
d. Compilation Error
Show Answer Workspace
4. Which of the following statements is correct about the ??= operator in C#?
a. It assigns the right operand to the left operand only if the left operand is null.
b. It always assigns the right operand to the left operand.
c. It checks if two operands are equal.
d. It throws an exception if the left operand is null.
Show Answer Workspace
5. What will be the output of the following bitwise operation?
int x = 5 & 3;
[Link](x);
a. 1
b. 3
c. 5
[Link] 12/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
d. 7
Show Answer Workspace
Next Topic C# Keywords
← prev next →
Related Posts
C# Example
: Hello World In C# programming language, a simple "hello world" program can be written by multiple ways. Let's
see the top 4 ways to create a simple C# example: Simple Example Using System Using public modifier Using namespace C#
Simple Example Example class Program { ...
3 min read
C# Features
C# is object oriented programming language. It provides a lot of features that are given below. Simple Modern programming
language Object oriented Type safe Interoperability Scalable and Updateable Component oriented Structured programming
language Rich Library Fast speed (adsbygoogle = [Link] || []).push({}); 1) Simple C# is a simple language in the
sense that it provides structured approach (to break...
2 min read
C# Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name etc. In C# keywords cannot be used as
identifiers. However, if we want to use the keywords as identifiers, we may prefix the keyword with @ character. A list of Reserved
Keywords...
1 min read
C# Variables
C# Variable A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many
times. It is a way to represent memory location through symbol so that it can be easily identified. The basic variable...
1 min read
C# History
History of C# language is interesting to know. Here we are going to discuss brief history of C# language. C# is pronounced as "C-
Sharp". It is an object-oriented programming language provided by Microsoft that runs on .Net Framework. Anders Hejlsberg is
known as the founder of C# language. It...
[Link] 13/16