Csharp-operators

Last modified on May 08th, 2020 by DigitalIndiaInfo Team.


C# Operators

Operators in C# are some special symbols that perform some action on operands. In mathematics, the plus symbol (+) do the sum of the left and right numbers. In the same way, C# includes different operators that act on operands.

The following example demonstrates the + operator in C#.

Example: + Operator
int x = 5 + 5;
int y = 10 + x;
int z = x + y;

In the above example, + operator adds two number literals and assign the result to a variable. It also adds the values of two int variables and assigns the result to a variable.

Some operators behave differently based on the type of the operands. For example, + operator concatenates two string literals or variables.

Example: + Operator with Strings
string greet1 = "Hello " + "World!";
string greet2 = greeting + name;
Note:
There are two types of operators in C#, Unary operators and Binary operators. Unary operators act on single operand, whereas binary operators act on two operands (left-hand side and right-hand side operand of an operator).

C# includes the following categories of operators:

Arithmetic Operators

The arithmetic operators perform arithmetic operations on all the numeric type operands such as sbyte, byte, short, ushort, int, uint, long, ulong, float, double, and decimal.

Operator Name Description Example
+ Addition Computes the sum of left and right operands. int x = 5 + 5; Try YourSelf
- Subtraction Subtract the right operand from the left operand int x = 5 - 1; Try YourSelf
* Multiplication Multiply left and right operand int x = 5 * 1; Try YourSelf
/ Division Divides the left operand by the right operand int x = 10 / 2; Try YourSelf
% Reminder Computes the remainder after dividing its left operand by its right operand int x = 5 % 2; Try YourSelf
++ Unary increment Unary increment ++ operator increases its operand by 1 x++ Try YourSelf
-- Unary decrement Unary decrement -- operator decreases its operand by 1 x-- Try YourSelf
+ Unary plus Returns the value of operand +5 Try YourSelf
- Unary minus Computes the numeric negation of its operand. -5 Try YourSelf

Assignment Operators

The assignment operator = assigns its right had value to its left-hand variable, property, or indexer. It can also be used with other arithmetic, Boolean logical, and bitwise operators.

Operator Name Description Example
= Assignment Assigns its right had value to its left-hand variable, property or indexer. x = 10; Try YourSelf
x op= y Compound assignment Short form of x =x op y where op = any arithmetic, Boolean logical, and bitwise operator. x += 5; Try YourSelf
??= Null-coalescing assignment C# 8 onwards, ??= assigns value of the right operand only if the left operand is null x ??= 5; Try YourSelf

Comparison Operators

Comparison operators compre two numeric operands and returns true or false.

Operator Description Example
< Returns true if the right operand is less than the left operand x < y; Try YourSelf
> Returns true if the right operand is greater than the left operand x > y; Try YourSelf
<= Returns true if the right operand is less than or equal to the left operand x <= y Try YourSelf
>= Returns true if the right operand is greater than or equal to the left operand x >= y; Try YourSelf

Equality Operators

The equality operator checks whether the two operands are equal or not.

Operator Description Example
== Returns true if operands are equal otherwise false. x == y; Try YourSelf
!= Returns true if operands are not equal otherwise false. x != y; Try YourSelf

Boolean Logical Operators

The Boolean logical operators perform a logical operation on bool operands.

Operator Description Example
! Reverses the bool result of bool expression. Returns false if result is true and returns true if result is false. !false Try YourSelf
& Computes the logical AND of its operands. Returns true only when both true, otherwise false. x & y; Try YourSelf
| Computes the logical OR of its operands. Returns true when any one result is true. x | y; Try YourSelf
^ Computes the logical exclusive of its operands. It returns true only if any one operand is true and another operand is false, otherwise returns false. x ^ y; Try YourSelf
&& Computes the logical AND of its bool operands. Returns true both operands are true, otherwise returns false. x && y; Try YourSelf
|| Computes the logical OR of its bool operands. Returns true when any one operand is true. x || y; Try YourSelf

Operator Evaluation & Precedence

Evaluation of the operands in an expression starts from left to right. If multiple operators are used in an expression, then the operators with higher priority are evaluated before the operators with lower priority.

The following table lists operators starting with the higher precedence operators to lower precedence operators.

Operators Category
x.y, x?.y, x?[y], f(x), a[i], x++, x--, new, typeof, checked, unchecked, default, nameof, delegate, sizeof, stackalloc, x->y Primary
+x, -x, !x, ~x, ++x, --x, ^x, (T)x, await, &x, *x, true and false Unary
x..y Range
x * y, x / y, x % y Multiplicative
x + y, x – y Additive
x << y, x >> y Shift
x < y, x > y, x <= y, x >= y, is, as Relational and type-testing
x == y, x != y Equality
x & y Boolean logical AND
x ^ y Boolean logical XOR
x | y Boolean logical OR
x && y Conditional AND
x || y Conditional OR
x ?? y Null-coalescing operator
c ? t : f Conditional operator
x = y, x += y, x -= y, x *= y, x /= y, x %= y, x &= y, x |= y, x ^= y, x <<= y, x >>= y, x ??= y, => Assignment and lambda declaration

The following example demonstrates operator precedence:

Example: Operator Precedence
int a = 5 + 3 * 3;
int b = 5 + 3 * 3 /  2; 
int c = (5 + 3) * 3 /  2; 
int d = (3 * 3) * (3 / 3 + 5);

Learn about C# operators in details.



References :

  • www.tutorial.digitalindiainfo.com