Assignment Operators in c#

Assignment Operators in c#

Assignment operators in the C# programming language are used to assign a user-defined numeric, text, or Boolean value to the declared variables of the parameter data type in a C# program. Assignment operators in the C# programming language are mostly used to combine assignment data type values ​​with arithmetic or bitwise data type operator operations.

Assignment Operators in c#

List of popular assignment operators in the C# programming language.

OperatorOperator NameAssignment DescriptionillustrationSimilar To
=Assignment symbolIt Used to Assigns the right-hand value to the user define variablep = 7p = 7
+=Add and assignIt used to Adds numeric value and assigns the output valuep += 3p = p + 3
-=Subtract and assignIt used to Subtracts numeric value and assigns the outputp -= 9p = p – 9
*=Multiply and assignIt used to Multiplies numeric value and assigns the final output valuep *= 8p = p * 8
/=Divide and assignIt used to Divides and assigns the numeric division value to outputp /= 5p = p / 5
%=Modulus and assignIt used to Computes remainder value and assigns the value to outputp %= 4p = p % 4
&=Bitwise AND and assignIt used to Performs bitwise AND operator and assigns the output valuep &= 2P =p & 2
|Bitwise OR and assignIt used to Performs Bitwise OR and assign the output valuep |= 7p = p | 7
^=Bitwise XOR and assignIt used to Performs bitwise XOR and assigns the value to outputp ^= 2p = p ^ 2
<<=Left shift and assignIt used to Performs Shifts bits to the left and assigns value to outputp <<= 3p = p << 3
>>=Right shift and assignIt used to Performs Shifts bits to the right and assigns value to output  p >>= 2p = p >> 2

Assignment operator examples in C#.

using System;

class AssignmentOperator

{

static void Main()

{

int p = 21;

p += 4; // This is similar to p = p + 4

Console.WriteLine($”p variable value after += operation – {p}”); // Result is – 25

p *= 3; // This is similar to p = p * 3

Console.WriteLine($”p variable value after *= operation – {p}”); // Result is – 75

p %= 2; // this is similar to p = p % 2

Console.WriteLine($”p variable value after %= operation – {p}”); // Result is – 1

p|= 2; // this is similar to p Bitwise OR operation (p = p | 2)

Console.WriteLine($”p variable value after |= operation – {p}”); // Result is – 3

}

}

Bitwise OR and Bitwise XOR examples in C#.

Using System;

class BitwiseORandBitwiseXOR

{

static void Main()

{

int p=9; // equivalent to Binary value – 1001

int q = 7; // equivalent to Binary value – 0111

// here we use Bitwise OR operator

int output = p | q;

Console.WriteLine(output); // Result is – 15

// here we use XOR Assignment operator

p^=q; // similar as – p = p ^ q

Console.WriteLine(p); // Result is – 14

}

}

Left shift and assign Right shift examples in C#.

Using System;

class LeftshiftandassignRightshift

{

static void Main()

{

int q = 8; // Binary equivalent value – 1000

// here we use Left Shift Assignment operator

q <<= 2; // similar as – q = q << 2

Console.WriteLine(q); // Result is – 32

// here we use Right Shift Assignment operator

q >>= 1; // similar as – q = q >> 1

Console.WriteLine(q); // Result is – 16

}

}

Important Notes in the C# Programming Language.

C# Chained Assignment Concept.

In a C# program, multiple parameter variables can be assigned the same value or assigned the same custom value within a single user-defined statement. They can be processed based on user-defined logic expressions within the program.

using System;

class ChainedAssignments

{

static void Main()

{

/ Here we are assigning the same value to multiple similar variables

int p, q, r;

p = q = r = 7;

Console.WriteLine(p + q + r); // Result is – 21

}

}

Assignment operators (+=, -=) are used in for loops.

In a C# program, you can use the += and -= assignment operators to traverse, iterate, or repeat the loop values ​​from the start to the end of a for loop as needed.

using System;

class AssignWithForloop

{

static void Main()

{

for (int p = 1; p <= 21; p += 3)

{

Console.WriteLine(p); // Output is – 1, 4 7, 10, 13, 16, 19

}

}

}

Leave a Reply