C#- Programming Guide | Starting Phase Learning(8)

Spread the love

Here, we will discuss the remaining operators in C#..

2.Relational Operators:

It compares two operands and returns true or false.

  • == Checks if the values of two operands are equal or not, if yes then condition becomes true.
  • != Checks if the values of two operands are equal or not, if values are not equal then the condition becomes true. Checks if the value of the left operand is greater than the value of right operand, if yes then condition becomes true.
  • < Checks if the value of the left operand is less than the value of right operand, if yes then condition becomes true.
  • = Checks if the value of the left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
  • <= Checks if the value of the left operand is less than or equal to

3.Logical Operators:

Logical operators perform a logical operation on bool operands.

  • && Called Logical AND operator. If both the operands are non zero then the condition becomes true.
  • || Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.
  • ! Called Logical NOT Operator.

example
a= 5;
b= 6;

a<b //this returns true , means if b is greater than a then it will return true(here it returns true )
!a<b //this returns false , means if b is not greater than a (here it returns true )

4.Bitwise Operators

Bitwise operator performs bit by bit operation.
We have various bitwise operators

  • & Binary AND Operator copies a bit to the result if it exists in both operands.
  • | Binary OR Operator copies a bit if it exists in either operand.
  • ^ Binary XOR Operator copies the bit if it is set in one operand but not both.
  • ~ Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.
  • << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
  • Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

Example
Let’s say,
A = 62; and B = 10;
Then converting into the binary format they are as follows −

A= 0011 1110
B= 0000 1010

Performinng Binary AND Operator (&):
A & B= 0000 1010

Performinng Binary OR Operator (|):
(A ^ B) = 0011 1110

Performinng Binary XOR Operator (^):
(A ^ B) = 0011 0100

Binary Ones Complement Operator
(~A )= 1100 0001

Performinng Binary Left Shift Operator (<<):
A << 2 = 1111 0010

Performinng Binary AND Operator (&):
A >> 2 = 0000 1111

5.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.
Assignment (=):
example :
int x = 7;
x op= y
Compound assignment:
Example:
int x +=8;
int a *=6;

6.Misc Operators

We have various Miscellaneous Operators in C# which we will discuss later ..)

  • typeof() Returns the type of a class
  • sizeof() Returns the size of a data type
  • & Returns the address of a variable
  • ? : Conditional ExpressionPointer to a variable
  • as Cast without raising an exception if the cast fails
  • is Determines whether an object is of a certain type

7.Unary operators:(++ , –)

Already discussed in Arithmetic operators. You can go to C#- Programming Guide | Starting Phase Learning(7)

8.Ternary Operators(?)

Comparison are made in a statement using ? and :
Example:
int a = 5;
int b = 10;
a = b>a ? 0 : 1;

Others listed operators will see in detail the next phase of C# Learning
default operator
delegate operator
nameof operator
new operator
sizeof operator
stackalloc operator
true and false operators
Operator overloading

**In the next article, we will see the conditional statement and operator uses in a real-time project.

2020-02-16

Leave a Reply

Your email address will not be published. Required fields are marked *