C#

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

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.

Ankit

Leave a Comment

Recent Posts

The Ultimate Guide to Choosing the Best Security Company in Noida

When it comes to security services in Noida, there are several reputable companies that cater to various needs. Let me…

1 year ago

Steel Bite Pro: The Revolutionary Supplement Transforming Oral Health Forever

Are you tired of dealing with tooth decay, gum problems, and expensive dental treatments? Look no further! Steel Bite Pro…

1 year ago

Captivating Elegance: Unveiling the Beauty of Oval Moissanite Rings

In the world of fine jewelry, oval moissanite rings have emerged as a captivating choice for those seeking elegance and…

1 year ago

Top 10 Digital Marketing Agency In Noida

Noida, located in the state of Uttar Pradesh, is a fast-growing hub of digital marketing agencies in India. Choosing the…

2 years ago

Two-Wheeled Thrills: The Ultimate Guide to the Latest Bikes in India 2023

As we move into 2023, the world of motorcycles in India is set to see some exciting changes. Manufacturers are…

2 years ago

Five new rules will change the game in IPL 2023

The Indian Premier League (IPL) is one of the most popular and exciting cricket tournaments in the world. With the…

2 years ago