Assignment operators in C#

(Last Updated On: January 29, 2019)

Assignment operators in C#- Learn about assignment operator in C# with example and easy explanation. Also, learn assignment operator combined with arithmetic operators in C# like +=, *= etc. with example.

= Assignment operator in C#

Assignment operator in C# program is used to assign a value to a variable.

For example,
int a = 10;
Also, value of one variable can be assigned to another variable. For example, if two variables are a and b, value of b can be assigned to a as

a = b; // right hand side value will be assigned to left hand variable.

Note that right hand side variable must be initialized, means, it must have some value.

 

NOTE: Assignment (=) operator can be combined with other operators in C# language.

Examples below.

+= , -=, *=, /=

e.g.
if variable is int a =10;
then n += 5; will result into 15.

 

+= C# Example

n += 5; is same as n = n+5.

C# Code Example

Output:

n =15

Similarly, others -=, *= and /= works

 

-= C# Example

note that n -= 5; is same as n=n-5;

Output:
n =5

 

*= C# example:

n *= 5; is same as n=n*5;

C# Code example

Output:
n =50

 

/= C# Examples

n /= 5; is same as n=n/5;

C# Code example

Output:
n =2

 

%= C# Examples

n %= 5; is same as n=n%5;

C# Code example

Output:
n =0