1.
Introduction
2.
Types of operators
2.1.
Arithmetic operators
2.1.1.
Unary operators
2.1.2.
Binary operators
2.1.3.
Ternary Operators
2.2.
Relational operators
2.3.
Logical operators
2.4.
Bitwise operators
2.5.
Assignment operators
2.6.
Conditional operator
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Operators in C#

Introduction

Operators are the core of any programming language, and they are defined as symbols that assist the programmer in performing certain mathematical and logical operations on operands. The operator '*', for example, is used for multiplication, as z= x * y;

The operator '*' is the multiplication operator, and the operands 'x' and 'y' are operands. The multiplication operator instructs the compiler to multiply both operands and store the result in the variable 'z.'

Without operators, any programming language is incomplete.

Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.

Types of operators

In C#, there are many types of operators which can be classified into the following:

Arithmetic operators

Arithmetic Operators are used for performing mathematical/arithmetic operations on the operands. Arithmetic operators are of the following types:

In the below image, we can see all the arithmetic operators with their descriptions.

Unary operators

Unary operators are operators that work with a single operand. For example: Increment operator (++) and decrement operator (--)

E.g.

``````int val=5;
++val: // val=6 ``````

Binary operators

Binary operators are operators that work with two operands. For example:  Division(/),Multiplication(*),Addition (+), Subtraction(-) .

E.g.:

``````int x=1;
int y=2;
int z=y-x; // 1``````

Ternary Operators

The conditional operator, also known as the ternary operator, is a decision-making operator in C# which takes three operands. It is the abbreviation for the if-else conditions.

A boolean condition is used to start the ternary operator. If this condition is true, the first statement after? will be executed; otherwise, the second statement after: will be executed.

Syntax:

Example:

``````int x = 20, y = 10;
var result = x > y ? "x is greater than y" : "x is less than y";``````

C# program to show the working of Arithmetic Operators:

``````using System;
namespace Arithmetic
{
class CN
{
// Main Function
static void Main(string[] args)
{

int result;
int x = 12, y = 6;

result = (x + y);
Console.WriteLine("x + y = "  + result);

// Subtraction
result = (x - y);
Console.WriteLine("x - y = "  + result);

// Multiplication
result = (x * y);
Console.WriteLine("x * y = "  + result);

// Division
result = (x / y);
Console.WriteLine("x / y = "  + result);

// Modulo
result = (x % y);
Console.WriteLine("x % y = "  + result);

// Increment
x++;
Console.WriteLine("x++  = "  + x);

// Decremental
y--;
Console.WriteLine("y--  = "  + y);
}
}
}``````

Output:

Relational operators

Relational operators are used to comparing the two operands' values and form a relationship between them. For example, If we determine whether one operand is greater than the other, or whether two operands are equal, and so on.

C# program to show the working of Relational Operators:

``````using System;
namespace Relational {

class CN {

// Main Function
static void Main(string[] args)
{
bool result;
int x = 2, y = 10;

// Equal to Operator
result = (x == y);
Console.WriteLine("x == y gives " + result);

// Greater than Operator
result = (x > y);
Console.WriteLine("x > y gives " + result);

// Less than Operator
result = (x < y);
Console.WriteLine("x < y gives " + result);

// Greater than Equal to Operator
result = (x >= y);
Console.WriteLine("x >= y gives "+ result);

// Less than Equal to Operator
result = (x <= y);
Console.WriteLine("x <= y gives "+ result);

// Not Equal To Operator
result = (x != y);
Console.WriteLine("x != y gives " + result);
}
}
}``````

Output:

Logical operators

Logical operators such as "and" and "or" are used to execute logical operations. Logical operators return boolean values based on boolean expressions (true and false). In decision-making and loops, logical operators are used.

For example: (2<6) && (2!=7) ;// true

C# program to show the working of Logical Operators:

``````using System;
namespace Logical {

class CN {

// Main Function
static void Main(string[] args)
{
bool a = true,b = false, result;

// AND operator
result = a && b;
Console.WriteLine("a && b is " + result);

// OR operator
result = a || b;
Console.WriteLine("a || b is "+ result);

// NOT operator
result = !a;
Console.WriteLine("!a is " + result);

}
}
}``````

Output:

Bitwise operators

On the operands, these operators are used to perform bit-level operations.

The operators are converted to bit-level first, and then operations on the operands are performed. If arithmetic operations are performed at the bit level, the results can be obtained faster. They only operate with integral data types; they don't work with floating-point numbers.

For example

``````int a=5; // a = 00000101
-a; // a=11111010``````

C# program to show the working of Bitwise Operators:

``````using System;
namespace Bitwise {

class CN {

// Main Function
static void Main(string[] args)
{
int x = 2, y = 3, result;

// Bitwise AND Operator
result = x & y;
Console.WriteLine("x & y: " + result);

// Bitwise OR Operator
result = x | y;
Console.WriteLine("x | y: " + result);

// Bitwise XOR Operator
result = x ^ y;
Console.WriteLine("x ^ y: " + result);

// Bitwise AND Operator
result = ~x;
Console.WriteLine("~x " + result);

// Bitwise LEFT SHIFT Operator
result = x << 2;
Console.WriteLine("x << 2: " + result);

// Bitwise RIGHT SHIFT Operator
result = x >> 2;
Console.WriteLine("x >> 2: " + result);

}
}
}``````

Output:

Assignment operators

These are used to assign values to variables by placing the variable on the left side of the operator and the value on the right side of the operator. Otherwise, the compiler will throw an error if the value on the right side of the operator is not of the same data type as the variable on the left side of the operator.

C# program to show the working of Assignment Operators:

``````using System;
namespace Assignment {

class CN {

// Main Function
static void Main(string[] args)
{
int x = 10;

// it means x = x + 5
x += 5;
Console.WriteLine(x);

// it means x = x - 2
x -= 2;
Console.WriteLine(x);

// it means x = x * 4
x *= 4;
Console.WriteLine(x);

// it means x = x / 4
x /= 4;
Console.WriteLine(x);
}
}
}``````

Output:

Conditional operator

• In this, an expression/condition is first evaluated and then acts based on the evaluation result.
• Syntax: Condition? Expression-1? Expression-2;
• In this first, we will evaluate the condition given, if the condition is true, then Expression-1 is executed, and if the condition is false, then Expression-2 is executed.
• We can replace if..else statements with these conditional operators.

C# program to demonstrate the working of Conditional Operator:

``````using System;
namespace Conditional {
class CN {
// Main Function
static void Main(string[] args)
{
int x = 15, y = 20, result;
// To find which is maximum among two numbers
result = x > y ? x : y;
Console.WriteLine("Max of x and y is : " + result);
}
}
}``````

Output:

FAQs

1. What is the difference between ++a and a++?
Ans: ++a and a++ are not interchangeable; ++a means increment first, then use, while a++ means use first, then increment.
2. What will be the value of a :
int a=5;
a+=4;
Ans: In +=, we first add the value of the left operand to the value of the right operand and then assign the value so a will 5+4 = 9, therefore 9 will be printed.
3. What's the difference between '=' and '=='?
Ans: No, '=' and '==' are not interchangeable. '=' is an assignment operator that assigns the value on the right to the variable on the left. '==' is a relational operator determining whether the LHS and RHS are equal.
4. How do bitwise operators work?
Ans: The operands are converted to bit-level first, and then operations on the operands are performed.
5. Where are logical operators mostly used?
In decision-making and loops, logical operators are used.

Key Takeaways

In this article, we have extensively discussed Operators and have seen their types and implementation in C#