in , ,

C# Operators

csharp Operators
csharp Operators

C# Operators: In this tutorial, we will learn everything about various types of operators in C# programming language and how to use them.

Operators are symbols that are used to perform operations on operands. Operands might be variables and /or constants.

Operators are used to perform operations on variables and values.

For instance, in 2+3, + is an operator that is used to carry out addition operation, while 2 and 3 are operands.

Operators are used to control variables and values in a program. C# supports a number of operators that are ordered dependent on the sort of operations they perform.


1. Basic Assignment Operator

Essential assignment operator (=) is used to assign values to variables. For instance,

double x;
x = 50.05;

Here, 50.05 is assigned to x.

Example 1: Basic Assignment Operator

using System;

namespace Operator
{
	class AssignmentOperator
	{
		public static void Main(string[] args)
		{
			int firstNumber, secondNumber;
			// Assigning a constant to variable
			firstNumber = 10;
			Console.WriteLine("First Number = {0}", firstNumber);

			// Assigning a variable to another variable
			secondNumber = firstNumber;
			Console.WriteLine("Second Number = {0}", secondNumber);
		}
	}
}

At the point when we run the program, the output will be:

First Number = 10
Second Number = 10

This is a basic example that demonstrates the use of task operator.

You may have seen the use of curly brackets { } in the example. We will examine about them in string designing. For the time being, simply remember that {0} is replaced by the first variable that follows the string, {1} is replaced by the second variable and so on.


2. Arithmetic Operators

Arithmetic operators are used to perform Arithmetic operations like addition, subtraction, multiplication, division, and so forth

For instance,

int x = 5;
int y = 10;
int z = x + y;// z = 15
OperatorOperator NameExample
+Addition Operator6 + 3 evaluates to 9
Subtraction Operator10 – 6 evaluates to 4
*Multiplication Operator4 * 2 evaluates to 8
/Division Operator10 / 5 evaluates to 2
%Modulo Operator (Remainder)16 % 3 evaluates to 1

Example 2: Arithmetic Operators

using System;
 
namespace Operator
{
	class ArithmeticOperator
	{
		public static void Main(string[] args)
		{
			double firstNumber = 14.40, secondNumber = 4.60, result;
			int num1 = 26, num2 = 4, rem;

			// Addition operator
			result = firstNumber + secondNumber;
			Console.WriteLine("{0} + {1} = {2}", firstNumber, secondNumber, result);

			// Subtraction operator
			result = firstNumber - secondNumber;
			Console.WriteLine("{0} - {1} = {2}", firstNumber, secondNumber, result);

			// Multiplication operator
			result = firstNumber * secondNumber;
			Console.WriteLine("{0} * {1} = {2}", firstNumber, secondNumber, result);

			// Division operator
			result = firstNumber / secondNumber;
			Console.WriteLine("{0} / {1} = {2}", firstNumber, secondNumber, result);

			// Modulo operator
			rem = num1 % num2;
			Console.WriteLine("{0} % {1} = {2}", num1, num2, rem);
		}
	}
}

At the point when we run the program, the output will be:

14.4 + 4.6 = 19
14.4 - 4.6 = 9.8
14.4 * 4.6 = 66.24
14.4 / 4.6 = 3.1304347826087
26 % 4 = 2

Arithmetic operations are carried out in the above example. Variables can be replaced by constants in the statement. For instance,

result = 4.5 + 2.7 ; // result will hold 7.2
result = firstNumber - 3.2; // result will hold 11.2

3. Relational Operators

Relational operators are used to check the relationship between two operands. On the off chance that the relationship is genuine the outcome will be true, else it will result in false.

Relational operators are used in decision making and loops.

OperatorOperator NameExample
==Equal to6 == 4 evaluates to false
>Greater than3 > -1 evaluates to true
<Less than5 < 3 evaluates to false
>=Greater than or equal to4 >= 4 evaluates to true
<=Less than or equal to5 <= 3 evaluates to false
!=Not equal to10 != 2 evaluates to true

Example 3: Relational Operators

using System;
 
namespace Operator
{
	class RelationalOperator
	{
		public static void Main(string[] args)
		{
			bool result;
			int firstNumber = 10, secondNumber = 20;

			result = (firstNumber==secondNumber);
			Console.WriteLine("{0} == {1} returns {2}",firstNumber, secondNumber, result);

			result = (firstNumber > secondNumber);
			Console.WriteLine("{0} > {1} returns {2}",firstNumber, secondNumber, result);

			result = (firstNumber < secondNumber);
			Console.WriteLine("{0} < {1} returns {2}",firstNumber, secondNumber, result);

			result = (firstNumber >= secondNumber);
			Console.WriteLine("{0} >= {1} returns {2}",firstNumber, secondNumber, result);

			result = (firstNumber <= secondNumber);
			Console.WriteLine("{0} <= {1} returns {2}",firstNumber, secondNumber, result);

			result = (firstNumber != secondNumber);
			Console.WriteLine("{0} != {1} returns {2}",firstNumber, secondNumber, result);
		}
	}
}

At the point when we run the program, the output will be:

10 == 20 returns False
10 > 20 returns False
10 < 20 returns True
10 >= 20 returns False
10 <= 20 returns True
10 != 20 returns True

4. Logical Operators

Logical operators are used to perform logical operations, for example, and, or. Logical operators works on boolean expressions (true and false) and returns boolean values. Logical operators are used in decision making and loops.

Here is the way the outcome is assessed for logical AND and or operators.

Operand 1Operand 2OR (||)AND (&&)
truetruetruetrue
truefalsetruefalse
falsetruetruefalse
falsefalsefalsefalse

In basic words, the table can be summed up as:

• If one of the operand is true, the OR operator will assess it to true.

• If one of the operand is false, the AND operator will assess it to false.

Example 4: Logical Operators

using System;
 
namespace Operator
{
	class LogicalOperator
	{
		public static void Main(string[] args)
		{
			bool result;
			int firstNumber = 10, secondNumber = 20;

			// OR operator
			result = (firstNumber == secondNumber) || (firstNumber > 5);
			Console.WriteLine(result);

			// AND operator
			result = (firstNumber == secondNumber) && (firstNumber > 5);
			Console.WriteLine(result);
		}
	}
}

At the point when we run the program, the output will be:

True
False

5. Unary Operators

Unlike other operators, the unary operators works on a single operand.

OperatorOperator NameDescription
+Unary PlusLeaves the sign of operand as it is
Unary MinusInverts the sign of operand
++IncrementIncrement value by 1
DecrementDecrement value by 1
!Logical Negation (Not)Inverts the value of a boolean

Example 5: Unary Operators

using System;
 
namespace Operator
{
	class UnaryOperator
	{
		public static void Main(string[] args)
		{
			int number = 10, result;
			bool flag = true;

			result = +number;
			Console.WriteLine("+number = " + result);

			result = -number;
			Console.WriteLine("-number = " + result);

			result = ++number;
			Console.WriteLine("++number = " + result);

			result = --number;
			Console.WriteLine("--number = " + result);

			Console.WriteLine("!flag = " + (!flag));
		}
	}
}

At the point when we run the program, the output will be:

+number = 10
-number = -10
++number = 11
--number = 10
!flag = False

The increment (++) and decrement (- – ) operators can be used as prefix and postfix. Whenever used as prefix, the adjustment of value of variable is seen on a similar line and whenever used as postfix, the adjustment of value of variable is seen on the next line. This will be clear by the example beneath.

Example 6: Post and Pre Increment operators in C#

using System;
 
namespace Operator
{
	class UnaryOperator
	{
		public static void Main(string[] args)
		{
			int number = 10;

			Console.WriteLine((number++));
			Console.WriteLine((number));

			Console.WriteLine((++number));
			Console.WriteLine((number));
		}
	}
}

At the point when we run the program, the output will be:

10
11
12
12

We can see the impact of using++ as prefix and postfix. When ++ is used after the operand, the worth is first assessed and afterward it is increased by 1. Henceforth the statement

Console.WriteLine((number++));

prints 10 rather than 11. After the value is printed, the value of number is increased by 1.

The process is opposite when ++ is used as prefix. The value is increased prior to printing. Subsequently the statement

Console.WriteLine((++number));

prints 12.

The case is same for decrement operator(- – ).


6. Ternary Operator

The ternary operator? : works on three operands. It is a shorthand for if-then-else statement. Ternary operator can be used as follows:

variable = Condition? Expression1 : Expression2;

The ternary operator works in as follows:

• If the expression expressed by Condition is true, the result of Expression1 is assigned to variable.

• If it is false, the result of Expression2 is assigned to variable.

Example 7: Ternary Operator

using System;
 
namespace Operator
{
	class TernaryOperator
	{
		public static void Main(string[] args)
		{
			int number = 10;
			string result;

			result = (number % 2 == 0)? "Even Number" : "Odd Number";
			Console.WriteLine("{0} is {1}", number, result);
		}
	}
}

At the point when we run the program, the output will be:

10 is Even Number

To learn more, visit C# ternary operator.


7. Bitwise and Bit Shift Operators

Bitwise and digit shift operators are used to perform bit control operations.

OperatorOperator Name
~Bitwise Complement
&Bitwise AND
|Bitwise OR
^Bitwise Exclusive OR
<<Bitwise Left Shift
>>Bitwise Right Shift

Example 8: Bitwise and Bit Shift Operator

using System;
 
namespace Operator
{
	class BitOperator
	{
		public static void Main(string[] args)
		{
			int firstNumber = 10;
			int secondNumber = 20;
			int result;

			result = ~firstNumber;
			Console.WriteLine("~{0} = {1}", firstNumber, result);

			result = firstNumber & secondNumber;
			Console.WriteLine("{0} & {1} = {2}", firstNumber,secondNumber, result);

			result = firstNumber | secondNumber;
			Console.WriteLine("{0} | {1} = {2}", firstNumber,secondNumber, result);

			result = firstNumber ^ secondNumber;
			Console.WriteLine("{0} ^ {1} = {2}", firstNumber,secondNumber, result);

			result = firstNumber << 2;
			Console.WriteLine("{0} << 2 = {1}", firstNumber, result);

			result = firstNumber >> 2;
			Console.WriteLine("{0} >> 2 = {1}", firstNumber, result);
		}
	}
}

At the point when we run the program, the output will be:

~10 = -11
10 & 20 = 0
10 | 20 = 30
10 ^ 20 = 30
10 << 2 = 40
10 >> 2 = 2

To learn more, visit C# Bitwise and Bit Shift operator.


8. Compound Assignment Operators

OperatorOperator NameExampleEquivalent To
+=Addition Assignmentx += 5x = x + 5
-=Subtraction Assignmentx -= 5x = x - 5
*=Multiplication Assignmentx *= 5x = x * 5
/=Division Assignmentx /= 5x = x / 5
%=Modulo Assignmentx %= 5x = x % 5
&=Bitwise AND Assignmentx &= 5x = x & 5
|=Bitwise OR Assignmentx |= 5x = x | 5
^=Bitwise XOR Assignmentx ^= 5x = x ^ 5
<<=Left Shift Assignmentx <<= 5x = x << 5
>>=Right Shift Assignmentx >>= 5x = x >> 5
=>Lambda Operatorx => x*xReturns x*x

Example 9: Compound Assignment Operator

using System;
 
namespace Operator
{
	class BitOperator
	{
		public static void Main(string[] args)
		{
			int number = 10;

			number += 5;
			Console.WriteLine(number);

			number -= 3;
			Console.WriteLine(number);

			number *= 2;
			Console.WriteLine(number);

			number /= 3;
			Console.WriteLine(number);

			number %= 3;
			Console.WriteLine(number);

			number &= 10;
			Console.WriteLine(number);

			number |= 14;
			Console.WriteLine(number);

			number ^= 12;
			Console.WriteLine(number);

			number <<= 2;
			Console.WriteLine(number);

			number >>= 3;
			Console.WriteLine(number);
		}
	}
}

At the point when we run the program, the output will be:

15
12
24
8
2
2
14
2
8
1

We will discuss about Lambda operators in later tutorial.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

csharp Variables (Primitive) Data Types

C# Variables and (Primitive) Data Types

csharp Basic Input and Output

C# Basic Input and Output