C# - C Sharp Operators - Tpoint Tech
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.
Example
using System;
class BasicExample {
static void Main() {
int a = 10, b = 20;
int c = a + b;
Output:
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.
https://www.tpointtech.com/csharp-operators 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.
Example
using System;
class Arithmetic {
static void Main() //main method
{
int a = 10, b = 3; //initializing variables and values
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
https://www.tpointtech.com/csharp-operators 2/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
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.
Example
using System;
class Relational
{
static void Main() //main method
{
int a = 15, b = 30; //initializing variables and values
https://www.tpointtech.com/csharp-operators 3/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Console.WriteLine("The value of a <= b is " + (a <= b)); // Less than or equal to operator
}
}
Output:
3. Logical Operators
In C#, logical operators are utilized to combine more than one condition and return a boolean result.
Example
using System;
class Logical
{
static void Main() //main method
{
bool x = true, y = false; //initializing Boolean variable and value
https://www.tpointtech.com/csharp-operators 4/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Output:
Python Java JavaScript SQL C C++ HTML CSS React Node js Spring Boot C#
4. Bitwise Operators
In C#, bitwise operators operate at the binary level to modify bits of a number.
Example
using System;
class Bitwise
{
https://www.tpointtech.com/csharp-operators 5/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Output:
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.
https://www.tpointtech.com/csharp-operators 6/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Example
using System;
class Program
{
static void Main() //main function
{
int a = 10, b = 6; //initializing variables and values
Console.WriteLine("The value of a+=b is " + (a += b)); //using Add and Assignment Operator
Console.WriteLine("The value of a-=b is " + (a -= b)); //using Subtract and Assignment Operator
Console.WriteLine("The value of a*=b is " + (a *= b)); //using Multiply and Assignment Operator
Console.WriteLine("The value of a/=b is " + (a /= b)); //using Divide and Assignment Operator
Console.WriteLine("The value of a%=b is " + (a %= b)); //using Modulus and Assignment Operator
}
}
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.
default.
Example
using System;
class Unary
{
static void Main() //main method
{
int a = 10; //initialize variable and value
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
https://www.tpointtech.com/csharp-operators 8/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Syntax:
It has the following syntax:
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.
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
Output:
https://www.tpointtech.com/csharp-operators 9/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Null Coalescing Assignment ??= It sets the left operand to the right
operand only if the left operand is null.
Example
using System;
class Program {
static void Main() {
int? num = null;
int result = num ?? 100; // If num is null, assign 100
int? x = null;
x ??= 50; // Assigns 50 to x if it is null
Output:
1. Operator Precedence
Operators with higher precedence are evaluated first. For example, in the expression:
Example
int result = 10 + 5 * 2;
Console.WriteLine(result);
https://www.tpointtech.com/csharp-operators 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
int result = 10 - 2 * 3 + 4;
Console.WriteLine(result);
Output:
Example
int a = 5;
int b = 10;
int result = a = b = 20; // Right to Left evaluation
Console.WriteLine(result);
Output:
20
int a = 5, b = 10, c;
c = a++ + --b;
https://www.tpointtech.com/csharp-operators 11/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
Console.WriteLine(c);
a. 14
b. 15
c. 16
d. 13
int a = 10;
int b = 20;
int c = 30;
bool result = (a < b) && (b > c) || (a < c);
Console.WriteLine(result);
a. True
b. False
c. Compilation Error
d. Runtime Error
int? x = null;
int y = x ?? 50;
Console.WriteLine(y);
a. null
b. 50
c. 0
d. Compilation Error
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.
int x = 5 & 3;
Console.WriteLine(x);
a. 1
b. 3
c. 5
https://www.tpointtech.com/csharp-operators 12/16
31/05/2025, 17:33 C# | C Sharp Operators - Tpoint Tech
d. 7
← 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 = window.adsbygoogle || []).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...
https://www.tpointtech.com/csharp-operators 13/16