C# Operators - Arithmetic, Comparison, Logical and More - PDF
C# Operators - Arithmetic, Comparison, Logical and More - PDF
C#
(/) Operators
Search tutorials & examples
www.domain-name.com
In this article, we will learn everything about different types of operators in C#
programming language and how to use them.
Operators are symbols that are used to perform operations on operands. Operands
may be variables and/or constants.
For example, in 2+3 , + is an operator that is used to carry out addition operation,
while 2 and 3 are operands.
double x;
x = 50.05;
First Number = 10
Second Number = 10
You might have noticed the use of curly brackets { } in the example. We will
discuss about them in string formatting. For now, just keep in mind 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 such as addition,
subtraction, multiplication, division, etc.
For example,
// Addition operator
result = firstNumber + secondNumber;
Console.WriteLine("{0} + {1} = {2}", firstNumber, second
// Subtraction operator
result = firstNumber - secondNumber;
Console.WriteLine("{0} - {1} = {2}", firstNumber, second
// Multiplication operator
result = firstNumber * secondNumber;
Console.WriteLine("{0} * {1} = {2}", firstNumber, second
// Division operator
result = firstNumber / secondNumber;
Console.WriteLine("{0} / {1} = {2}", firstNumber, second
// Modulo operator
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 statements. For example,
C# Relational Operators
result = (firstNumber==secondNumber);
Console.WriteLine("{0} == {1} returns {2}",firstNumber,
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 operation such as and , or . Logical
operators operates on boolean expressions ( true and false ) and returns boolean
Try hands-on
values. Logical operators are used(https://programiz.pro?utm_source=programiz-top-
in decision making and loops.
CODING coding with bar&utm campaign=programiz&utm medium=referral)
g bar&utm_campaign programiz&utm_medium referral)
Hereyou
PRO
Thank is how the result
for printing
Programiz is evaluated
our content
PRO for logical AND andPlease
at www.domain-name.com. OR operators.
check back soon for new
contents.
36% OFF Claim Discount
Now C# Logical operators
If one of the operand is false, the AND operator will evaluate it to false .
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);
}
}
}
5. Unary Operators
C# unary operators
result = +number;
Console.WriteLine("+number = " + result);
result = -number;
Console.WriteLine("-number = " + result);
result = ++number;
Console.WriteLine("++number = " + result);
result = --number;
Console.WriteLine("--number = " + result);
+number = 10
-number = -10
++number = 11
--number = 10
!flag = False
The increment (++) and decrement (--) operators can be used as prefix and
postfix. If used as prefix, the change in value of variable is seen on the same line
and if used as postfix, the change in value of variable is seen on the next line. This
will be clear by the example below.
Console.WriteLine((number++));
Console.WriteLine((number));
Console.WriteLine((++number));
Console.WriteLine((number));
}
}
}
10
11
12
12
We can see the effect of using ++ as prefix and postfix. When ++ is used after the
operand, the value is first evaluated and then it is incremented by 1 . Hence the
statement
Console.WriteLine((number++));
The process is opposite when ++ is used as prefix. The value is incremented before
printing. Hence the statement
Console.WriteLine((++number));
prints 12 .
Try hands-on (https://programiz.pro?utm_source=programiz-top-
CODING coding with bar&utm campaign=programiz&utm medium=referral)
g bar&utm_campaign programiz&utm_medium referral)
The you
PRO
Thank caseforisprinting
same for
ourdecrement
Programiz content
PRO operator (--) .
at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
using System;
namespace Operator
{
class TernaryOperator
{
public static void Main(string[] args)
{
int number = 10;
string result;
10 is Even Number
~ Bitwise Complement
| Bitwise OR
^ Bitwise Exclusive OR
result = ~firstNumber;
Console.WriteLine("~{0} = {1}", firstNumber, result);
~10 = -11
10 & 20 = 0
10 | 20 = 30
10 ^ 20 = 30
10 << 2 = 40
10 >> 2 = 2
/= Division Assignment x /= 5 x = x / 5
%= Modulo Assignment x %= 5 x = x % 5
|= Bitwise OR Assignment x |= 5 x = x | 5
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 |= 14;
15
12
24
8
2
2
14
2
8
1
Search tutorials
Previous & examples
Tutorial:
(/)
(/csharp-programming/variables-primitive-data-types)
C# Variables www.domain-name.com
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/csharp- text=Check%20this%20amazing%20
programming/operators) programming/operators)
Related Tutorials
C# Tutorial
(/csharp-programming/bitwise-operators)
C# Tutorial
(/csharp-programming/operator-precedence-associativity)
C# Tutorial
C# ternary (? :) Operator