Assignment operators are used to assign values to variables. This type of statement consists of a variable name, an assignment operator, and an expression. When appropriate, you can declare a variable and assign a value to it in a single statement. In assignment expressions, the right-hand expression is contextually typed by the type of the left hand expression. The first assignment operator in the table, assigns the value (unmodified) of the expression on the right of the equal sign to the variable on the left. The other five operators are called compound assignment operators that perform the specified operation on the right-side value before assiging a value to the left side.

The Assignment operators are summarized in the following table.

Operator

Same AS

Description

x = y

x = y

Simple assignment operator; assigns the value from the right side operand to the left side operand

x += y

x =x + y

Add AND assignment operator; it adds the right operand to the left operand and assigns the result to the left operand

x -= y

x =x - y

Subtract AND assignment operator; it subtracts the right operand from the left operand and assigns the result to the left operand

x *= y

x =x * y

Multiply AND assignment operator; it multiplies the right operand with the left operand and assigns the result to the left operand

x /= y

x =x / y

Divide AND assignment operator; it divides the left operand by the right operand and assigns the result to the left operand

x %= y

x =x % y

Modulus AND assignment operator; it divides the left operand by the right operand and assigns the remainder to the left operand

Example

The following example shows the assignment operation. In this example I have an assignment class and define an operator function in the class which perform various types of operations. Let's use the following steps.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown. Give the name of your application as "assignment-operator" and then click ok.

Step 2

After this session the project has been created. A new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file and html file.