Assignment Operators in C#
Notes:
Assignment Operators in C# Programming Language:
Assignment Operator: ( = )
- is used to assign a value to a variable or a constant
Short Hand Assignment (SHA) Operators:
- allow us to write compact or condensed form assignment statements
AA (Arithmetic Assignment) operators:
+=
-=
*=
/=
%=
BA (Bitwise Assignment) operators:
&=
|=
^=
<<=
>>=
Lambda operator:
=>
Example code:
using System;
namespace AssignmentOperatorsDemo
{
class Program
{
static void Main(string[] args)
{
const float PI = 3.142f;
Console.WriteLine(PI); // 3.142
int a = 0;
Console.WriteLine(a); // 0
a = 10;
Console.WriteLine(a); // 10
a += 10; // a = a + 10;
Console.WriteLine(a); // 20
a -= 10; // a = a - 10;
Console.WriteLine(a); // 10
a *= 2; // a = a * 2;
Console.WriteLine(a); // 20
a /= 2; // a = a / 2
Console.WriteLine(a); // 10
a %= 2; // a = a % 2;
Console.WriteLine(a); // 0
Console.ReadKey();
}
}
}