I'm confused with the syntax of C#: what is the use of "+="?
The +=
syntax can be used in different ways:
SomeEvent += EventHandler;
Adds a handler to an event.
SomeVariable += 3;
Is equivalent to
SomeVariable = SomeVariable + 3;
This is called a compound operator. There common to all languages I can thing of - Javascript, C, Java, PHP, .net, GL etc.
Like everyone has said, is a shorten'd version of value = value + 3
.
There are multiple reasons for its use. Most obviously its quicker to write, easier to read and faster to spot errors with.
Most importantly, a compound operator is specifically designed not to require as must computation as the equivalent value = value + 3
I'm not totally sure why but the evidence is paramount.
Simply create a loop, looping for say 5,000,000 adding a value as you proceed. In two test cases, I know personally from Actionscript there is roughly an 60% speed increase with compound operators.
You also have the equivalent:
+= addition
-= subtraction
/= multiplication
*= multplication
%= modulus
and the lesser obvious
++ Plus one
-- Minus one
Hope it helps buddy.
note it is not necessarily always equivalent.
for ordinary variables, a+=a
is, indeed, equivalent to a=a+a
(and shorter!). for the odd variable which changes its state, not so much.