Assignment Operator

What Does Assignment Operator Mean?

An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.

Advertisements

Unlike in C++, assignment operators in C# cannot be overloaded directly, but the user-defined types can overload the operators like +, -, /, etc. This allows the assignment operator to be used with those types.

Techopedia Explains Assignment Operator

The following are the characteristics of assignment operators:

  • When using the "=" operator for an assignment with the left operand as the property or indexer access, the property or indexer must have a set accessor.
  • Overloading a binary operator implicitly overloads its corresponding assignment operator (if any).
  • The different assignment operators are based on the type of operation performed between two operands such as addition (+=), subtraction, (-=), etc. The meaning of the operator symbol used depends on the type of the operands.
  • Assignment operators are right-associative, which means they are grouped from right to left.
  • Although assignment using assignment operator (a += b) achieves the same result as that without ( =a +b), the difference between the two ways is that unlike in the latter example, "a" is evaluated only once.
  • The assignment operator usually returns a reference to the object so as to be used in multiple assignments made in a single statement such as "a=b=c", where a, b and c are operands.
  • The assignment operator expects the type of both the left- and right-hand side to be the same for successful assignment.

In C#, an expression using an assignment operator might be "x op y", where x and y are operands and "op" represents the operator. The simple assignment operator "=" is used to store the value of its right-hand operand into the memory location denoted by the left-hand operand. The result is its return value. The other assignment operators that perform indicated operation on the two operands and assign a resulting value to the left operand are called compound assignment operators. These include:

  • +=
  • -=
  • *=
  • /=
  • %=
  • &=
  • |=
  • ^=
  • <<= and >>=
Advertisements

Related Terms

Latest DevOps Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…