Value '+ ='

I am confused with C # syntax: what is "+ ="?

+4
source share
4 answers

Syntax += can be used in different ways:

 SomeEvent += EventHandler; 

Adds a handler to the event.


 SomeVariable += 3; 

Is equivalent

 SomeVariable = SomeVariable + 3; 
+30
source

This is called a complex statement. They are common to all languages ​​that I can use: Javascript, C, Java, PHP, .net, GL.

As everyone said, this is an abridged version of value = value + 3 .

There are several reasons for using it. Most obviously, writing is faster, reading is easier, and errors are detected faster.

Most importantly, the complex operator is specially designed so as not to require as many calculations as the equivalent of value = value + 3 . I'm not quite sure why, but evidence is of the utmost importance.

Just create a loop by looping, say, 5,000,000, adding a value as you continue. In two test cases, I know personally from Actionscript, with about a 60% increase in speed with compound statements.


You also have an equivalent:

+= : addition

-= : subtraction

/= : multiplication

*= : multplication

%= : module

and all the less obvious:

++ : plus one

-- : minus

+6
source
 a += 3 

coincides with

 a = a + 3 
+5
source

Please note that this is not necessarily always equivalent.

for ordinary variables a+=a really equivalent to a=a+a and shorter !. For an odd variable that changes state, not so much.

0
source

Source: https://habr.com/ru/post/1304773/


All Articles