Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the symbols +=, *=,-=

Tags:

c#

These operands may be simple but the difficulty of finding explanations which are definitive and complete prompted me to ask. What are the character combinations containing an operand followed by an equal sign (such as *=, -=,+=, etc), what do they do and how are they useful (especially pertaining to non-numeric fields)?

Examples as well as definitions would be greatly appreciated.

Thanks

like image 784
user398296 Avatar asked Jul 27 '10 15:07

user398296


1 Answers

They are usually interpreted broadly as:

x += y    ===    x = x + y

(etc for your choice of operator)

however; some languages allow you to have a bespoke += operator*, or may interpret it differently in some scenarios; for example, in C# events, += and -= mean "subscribe via the add accessor" and "unsubscribe via the remove accessor" respectively.

Typically they are just space savers, but there can be semantic differences in some cases.


*=where I mean: very different to just the + operator and assignment

like image 159
Marc Gravell Avatar answered Oct 09 '22 13:10

Marc Gravell