0% found this document useful (0 votes)
172 views17 pages

C# Operators - Arithmetic, Comparison, Logical and More - PDF

The document discusses different types of operators in C# programming language. It describes basic assignment, arithmetic, and relational operators. It provides examples of how each operator is used by assigning values to variables and checking relationships between operands. The key points are: 1) Operators are used to manipulate variables and values. Basic assignment (=) assigns values, arithmetic operators perform math operations, and relational operators check relationships. 2) Examples demonstrate using each operator type, such as adding two numbers and checking if one is greater than the other. 3) Operators are essential elements that allow programming logic and control flow through assignments and condition checks.

Uploaded by

Clarent Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views17 pages

C# Operators - Arithmetic, Comparison, Logical and More - PDF

The document discusses different types of operators in C# programming language. It describes basic assignment, arithmetic, and relational operators. It provides examples of how each operator is used by assigning values to variables and checking relationships between operands. The key points are: 1) Operators are used to manipulate variables and values. Basic assignment (=) assigns values, arithmetic operators perform math operations, and relational operators check relationships. 2) Examples demonstrate using each operator type, such as adding two numbers and checking if one is greater than the other. 3) Operators are essential elements that allow programming logic and control flow through assignments and condition checks.

Uploaded by

Clarent Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

g bar&utm_campaign programiz&utm_medium referral)

PRO you for printing


Thank Programiz
our content
PRO at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now

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.

Operators are used to manipulate variables and values in a program. C# supports a


number of operators that are classified based on the type of operations they
perform.

1. Basic Assignment Operator


Basic assignment operator (=) is used to assign values to variables. For example,

double x;
x = 50.05;

Here, 50.05 is assigned to x.

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)
Example
PRO
Thank you for1: Basicour
printing Assignment
Programiz content
PRO Operator
at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
using System;
Search tutorials & examples
namespace
(/) Operator
{
www.domain-name.com
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);
}
}
}

When we run the program, the output will be:

First Number = 10
Second Number = 10

This is a simple example that demonstrates the use of assignment operator.

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,

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)
PRO you for printing
Thank Programiz
our content
PRO at www.domain-name.com. Please check back soon for new
int x = 5;
contents.
36% Claim Discount
intOFF
y = 10;
int z = x +Nowy;// z = 15

Search tutorials & examples


(/)
C# Arithmetic Operators
www.domain-name.com

Operator Operator Name Example

+ Addition Operator 6 + 3 evaluates to 9

- Subtraction Operator 10 - 6 evaluates to 4

* Multiplication Operator 4 * 2 evaluates to 8

/ Division Operator 10 / 5 evaluates to 2

% Modulo Operator (Remainder) 16 % 3 evaluates to 1

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)
Example
PRO
Thank you for2: Arithmetic
printing
Programiz PRO Operators
our content at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
using System;
Search tutorials & examples
namespace
(/) Operator
{
www.domain-name.com
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, 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

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 statements. For example,

result = 4.5 + 2.7 ; // result will hold 7.2


result = firstNumber - 3.2; // result will hold 11.2

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)
3. Relational
PRO
Thank you for printing Operators
Programiz
our content
PRO at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
Relational operators are used to check the relationship between two operands. If
the relationship
Searchistutorials
true the & result will be
examples true , otherwise it will result in false .
(/)
Relational operators are used inwww.domain-name.com
decision making and loops.

C# Relational Operators

Operator Operator Name Example

== Equal to 6 == 4 evaluates to false

> Greater than 3 > -1 evaluates to true

< Less than 5 < 3 evaluates to false

>= Greater than or equal to 4 >= 4 evaluates to true

<= Less than or equal to 5 <= 3 evaluates to false

!= Not equal to 10 != 2 evaluates to true

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)
Example
PRO
Thank you for3: Relational
printing
Programiz PROOperators
our content at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
using System;
Search tutorials & examples
namespace
(/) Operator
{
www.domain-name.com
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,

result = (firstNumber > secondNumber);


Console.WriteLine("{0} > {1} returns {2}",firstNumber, s

result = (firstNumber < secondNumber);


Console.WriteLine("{0} < {1} returns {2}",firstNumber, s

result = (firstNumber >= secondNumber);


Console.WriteLine("{0} >= {1} returns {2}",firstNumber,

result = (firstNumber <= secondNumber);


Console.WriteLine("{0} <= {1} returns {2}",firstNumber,

result = (firstNumber != secondNumber);


Console.WriteLine("{0} != {1} returns {2}",firstNumber,

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 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

Search tutorials & examples


(/)
Operand 1 Operand 2 OR (||) AND (&&)
www.domain-name.com
true true true true

true false true false

false true true false

false false false false

In simple words, the table can be summarized as:

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

If one of the operand is false, the AND operator will evaluate 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);
}
}
}

When we run the program, the output will be:


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)
PRO you for printing
Thank Programiz
our content
PRO at www.domain-name.com. Please check back soon for new
True
contents.
36% OFF Claim Discount
False
Now

Search tutorials & examples


(/)
www.domain-name.com

5. Unary Operators

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

C# unary operators

Operator Operator Name Description

+ Unary Plus Leaves the sign of operand as it is

- Unary Minus Inverts the sign of operand

++ Increment Increment value by 1

-- Decrement Decrement value by 1

! Logical Negation (Not) Inverts the value of a boolean

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)
Example
PRO
Thank you for5: Unary
printing ourOperators
Programiz content
PRO at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
using System;
Search tutorials & examples
namespace
(/) Operator
{ www.domain-name.com
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));


}
}
}

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. 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.

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)
Example
PRO
Thank you for6: Post our
printing andcontent
Programiz Pre Increment
PRO operators in Please
at www.domain-name.com. C# check back soon for new
contents.
36% OFF Claim Discount
Now
using System;
Search tutorials & examples
namespace
(/) Operator
{
www.domain-name.com
class UnaryOperator
{
public static void Main(string[] args)
{
int number = 10;

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

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

When we run the program, the output will be:

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++));

prints 10 instead of 11 . After the value is printed, the value of number is


incremented by 1 .

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

Search tutorials & examples


(/)
6. Ternary Operator www.domain-name.com

The ternary operator ? : operates 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 as follows:

If the expression stated 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);
}
}
}

When we run the program, the output will be:

10 is Even Number

To learn more, visit C# ternary operator.


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)
7. Bitwise
PRO
Thank andour
you for printing
ProgramizBit ShiftatOperators
content
PRO www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
Bitwise and bit shift operators are used to perform bit manipulation operations.
Search tutorials & examples
(/) C# Bitwise and Bit Shift operators
www.domain-name.com
Operator Operator Name

~ Bitwise Complement

& Bitwise AND

| Bitwise OR

^ Bitwise Exclusive OR

<< Bitwise Left Shift

>> Bitwise Right Shift

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)
Example
PRO
Thank you for8: Bitwise
printing
Programiz and BitatShift
our content
PRO Operator
www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
Now
using System;
Search tutorials & examples
namespace
(/) Operator
{
www.domain-name.com
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,secondN

result = firstNumber | secondNumber;


Console.WriteLine("{0} | {1} = {2}", firstNumber,secondN

result = firstNumber ^ secondNumber;


Console.WriteLine("{0} ^ {1} = {2}", firstNumber,secondN

result = firstNumber << 2;


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

result = firstNumber >> 2;

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

Try hands-on C# Compound Assignment Operators


(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)
PRO
Thank you for printing
Operator Programiz
our content
PROName
Operator at www.domain-name.com. Please checkEquivalent
Example back soonTofor new
contents.
36% OFF Claim Discount
+= Now Addition Assignment
x += 5 x = x + 5
Search tutorials & examples
-= (/) Subtraction Assignment x -= 5 x = x - 5
www.domain-name.com
*= Multiplication Assignment x *= 5 x = x * 5

/= Division Assignment x /= 5 x = x / 5

%= Modulo Assignment x %= 5 x = x % 5

&= Bitwise AND Assignment x &= 5 x = x & 5

|= Bitwise OR Assignment x |= 5 x = x | 5

^= Bitwise XOR Assignment x ^= 5 x = x ^ 5

<<= Left Shift Assignment x <<= 5 x = x << 5

>>= Right Shift Assignment x >>= 5 x = x >> 5

=> Lambda Operator x => x*x Returns x*x

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)
Example
PRO
Thank you for9: Compound
printing
Programiz PRO Assignment
our content Operator Please check back soon for new
at www.domain-name.com.
contents.
36% OFF Claim Discount
Now
using System;
Search tutorials & examples
namespace
(/) Operator
{ www.domain-name.com
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;

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.

Try hands-on (https://programiz.pro?utm_source=programiz-top-


CODING Nextwith
coding Tutorial:
bar&utm campaign=programiz&utm medium=referral)
g bar&utm_campaign programiz&utm_medium referral)
C# Operator
PRO you for printing
Thank Programiz
our content
PRO (/csharp-programming/operator-precedence-
at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount associativity)
Precedence
Now

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)

Did you find this article helpful?

Related Tutorials

C# Tutorial

C# Bitwise and Bit Shift Operators

(/csharp-programming/bitwise-operators)

C# Tutorial

C# Operator Precedence and Associativity

(/csharp-programming/operator-precedence-associativity)

C# Tutorial

C# ternary (? :) Operator

Try hands-on (https://programiz.pro?utm_source=programiz-top-


(/csharp-programming/ternary-operator)
CODING coding with bar&utm campaign=programiz&utm medium=referral)
g bar&utm_campaign programiz&utm_medium referral)
PRO
Thank
C# you for printing
Tutorial Programiz
our content
PRO at www.domain-name.com. Please check back soon for new
contents.
36% OFF Claim Discount
C# Basic InputNowand Output

Search tutorials & examples


(/)
www.domain-name.com
(/csharp-programming/basic-input-output)

You might also like