An operator is a special symbol that instructs the .NET system to perform a specific operation.
The operation could be addition, deletion, increment and so on. C# supports a vast list of operators.
Below is a list of commonly used operators in C#:
Unleashing the C# Operators
Lets dive into details and find out more about C# operators.
= Operator
Assignment operator is used to assign values to variables.
int NumberVariable = 10;
++ Operator
Increment operator increases the value of a numeric variable by 1.
Increment operator can be used before or after a variable name.
int NumberVariable = 10; NumberVariable++; System.Console.WriteLine(NumberVariable); ++NumberVariable; System.Console.WriteLine(NumberVariable);
Output:
11
12
12
-- Operator
Decrement operator decreases the value of a numeric variable by 1.
Like increment operator, decrement operator can be used before or after a variable name.
int NumberVariable = 10; NumberVariable--; System.Console.WriteLine(NumberVariable); --NumberVariable; System.Console.WriteLine(NumberVariable);
Output:
9
8
8
+ Operator
When used with numeric values + operator adds two values.
Concatenation happens when + operator is used with strings or used with a string and a numeric value.
int firstNumber = 1; int secondNumber = 2; string firstString = "Welcome "; string secondString = "Visitor"; System.Console.WriteLine(firstNumber + secondNumber); System.Console.WriteLine(firstString + secondString); System.Console.WriteLine(firstString + firstNumber);
Output:
3
Welcome Visitor
Welcome 1
Welcome Visitor
Welcome 1
By default a numeric value is always positive. However, + operator can be used to explicitly show a positive numeric value.
int positiveNumber = +1; System.Console.WriteLine(positiveNumber);
Output:
1
- Operator
When used with numeric values - operator subtracts one value from other.
It is also used for negative numbers.
int firstNumber = 1; int secondNumber = 2; System.Console.WriteLine(firstNumber - secondNumber); System.Console.WriteLine(secondNumber - firstNumber);
Output:
-1
1
1
* Operator
Multiplication (*) operator is used to compute the product of two numeric values.
long firstNumber = 3; long secondNumber = 2; System.Console.WriteLine(firstNumber * secondNumber);
Output:
6
/ Operator
Division (/) operator divides its operands.
double firstNumber = 3; double secondNumber = 2; System.Console.WriteLine(firstNumber / secondNumber);
Output:
1.5
% Operator
% operator is used to compute the remainder after dividing one numeric value with another.
double firstNumber = 3; double secondNumber = 2; System.Console.WriteLine(firstNumber % secondNumber);
Output:
1
. Operator
Dot operator is used to access members of an Struct or Class.
Below example shows how the ReadLine member of Console Class is accessed using the dot operator.
System.Console.ReadLine();
() Operator
If there are multiple operations to be performed in an expression then by using Parentheses operator the order of the operations can be specified.
int Number1 = 1; int Number2 = 3; int result = (Number2 - Number1) + Number2; System.Console.WriteLine(result );
Output: 5
Parentheses operator is also used while doing type conversions and while calling Methods or Delegates.
A type conversion is done as below:
uint xString = 21; int result = (int)xString;
Using the Parentheses operator a method is called or delegate is invoked as shown in this example:
MethodA();
[] Operator
When using arrays, indexers and attributes square bracket [] operator is used.
Arrays are created using square brackets as below:
int[] numberArray; numberArray = new int[10]; System.Console.WriteLine(numberArray.Length);
Indexers are used to access elements of an Array.
numberArray[0] = 21; System.Console.WriteLine(numberArray[0]);
Output:
21
Square brackets are also used while specifying C# Attributes.
[Conditional("TRACE_ON")] void Example() { string customer = "John Doe"; System.Console.WriteLine(customer); }
bool booleanNegate = false; System.Console.WriteLine(!booleanNegate);
! Operator
Negation (!) operator is used to negate its operands value. It is applicable on boolean values.
bool booleanNegate = false; System.Console.WriteLine(!booleanNegate);
Output:
True
!= Operator
Inequality (!=) operator returns true if its operands values are not equal. If the values are equal it returns false.
int firstValue = 3; int secondValue = 4; System.Console.WriteLine(firstValue != secondValue);
Output:
True
== Operator
Equality (==) operator returns true if its operands values are equal. If the values are not equal it returns true.
int firstValue = 3; int secondValue = 3; System.Console.WriteLine(firstValue == secondValue);
Output:
False
&& Operator
Conditional AND operator performs a logical AND of its operands.
The AND operator can only be used with operands which return a boolean true or false value. The second operand is evaluated if the first operand returns true.
int firstVal = 3; int secondVal = 4; int thirdVal = 5; int fourthVal = 5; System.Console.WriteLine((firstVal != secondVal) && (thirdVal == fourthVal));
Output:
True
|| Operator
Conditional OR operator performs a logical OR of its operands.
The OR operator can only be used with operands which return a boolean true or false value.
int firstVal = 3; int secondVal = 4; int thirdVal = 5; int fourthVal = 5; System.Console.WriteLine((firstVal == secondVal) || (thirdVal == fourthVal));
Output:
True
?: Operator
Ternary operator is used to take decisions based on a condition. They are also called as conditional operators.
condition ? expression1 : expression2; |
In the above expression ternary operator is used. Here if the condition is true then expression1is executed or else expression2 is executed.
Below example shows how a ternary operator can be used in a program.
int a = 2; int b = 3; var x = a > b ? True : False; Console.WriteLine("Value of x is " + x);
Output:
False
< Operator
The 'less than' (<) relational operator returns true if the first operand is less than the second operand.
int firstVal = 3; int secondVal = 4; System.Console.WriteLine((firstVal < secondVal));
Output:
True
> Operator
The 'greater than' (>) relational operator returns true if the first operand is greater than the second operand.
int firstVal = 3; int secondVal = 4; System.Console.WriteLine((secondVal > firstVal));
Output:
True
>= Operator
The 'greater than or equal' (>=) relational operator returns true if the first operand is greater than or equal to the second operand.
int firstVal = 3; int secondVal = 4; System.Console.WriteLine((secondVal >= firstVal)); firstVal = 3; secondVal = 3; System.Console.WriteLine((secondVal >= firstVal));
Output:
True
True
True
<= Operator
The 'less than or equal' (<=) relational operator returns true if the first operand is less than or equal to the second operand.
int firstVal = 3; int secondVal = 4; System.Console.WriteLine((firstVal <= secondVal)); firstVal = 3; secondVal = 3; System.Console.WriteLine((firstVal <= secondVal));
Output:
True
True
True