The truth is that it causes me a lot of confusion to see this type of operators, I don't know what it means or why it is placed.
checkedit1.CheckStateChanged += CheckValorSi_CheckStateChanged;
The truth is that it causes me a lot of confusion to see this type of operators, I don't know what it means or why it is placed.
checkedit1.CheckStateChanged += CheckValorSi_CheckStateChanged;
In your case
checkedit1.CheckStateChanged
you are subscribing to the eventCheckValorSi_CheckStateChanged;
through the operator+=
See examples here:
is the same as
Etc ...
so
An expression that uses the += operator , such as
It is equivalent to
except that x is only evaluated once. The meaning of the + operator depends on the types of x and y (addition for numeric operands, concatenation for string operands, etc.).
The += operator cannot be directly overloaded, that is, it must be initialized with a value initially.
The += operator is also used to specify a method that will be called in response to an event; such methods are called event handlers. Using the += operator in this context is called event subscription .
Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-assignment-operator
As other colleagues have commented, the += operator is a cumulative operator, what is on the right is accumulated (or added, if it is numerical data) to what is on the left.
In that specific case, what is being done is "accumulating" a delegate (what is on the right) to an event (what is on the left).
You can "accumulate/add" delegates to an event, and every time the event fires, all delegates that "accumulate/add" to the event will be executed. The thing on the right is a delegate, that is, a method.
You can also "accumulate/add" anonymous delegates to the event:
You can also "remove/subtract" delegates to an event:
Very useful to avoid memory leaks (memory leaks) that can occur under certain conditions.
When you put a button on a form and you double-click it, the form designer does what you put, creates a delegate (method) that usually calls it something like "private void button_Click" and accumulates it in the Click event of the button that you double-clicked it, only the code it generates where it puts the += puts it in another file.
Events are not only used in winforms, they are part of the content that any class can have, such as properties, fields or methods. You can define an event yourself in your classes, without even using winforms.
Finally, try to read the documentation that other colleagues put, it is really necessary to know about events and delegates, especially creating applications with GUI.
The += operator indicates that the variable is to be added to itself. It is equivalent to this:
This is an addition assignment. Adds the value of the variable from the right to the left. If both are numbers they are added, if at least one is a string they are concatenated.
Here is more information about the different operators: Expressions and operators
Assignment Operators
Known as assignment operators , they were created at the dawn of programming with the intention of simplifying (abbreviating) standard arithmetic operations. They are currently supported by most programming languages.
Operators Table
They all operate under similar semantics.
Example
And as always there is one exception to the rule and that is the assignment operator for addition
+=
, which is also used to concatenate (join) variable strings.Example
In some modern object-oriented programming languages, it has been given a new, somewhat orthodox connotation (Microsoft always takes the lead), and that is subscribing to events , which is more explained in previous answers.
It is the equivalent of writing: a = a + b; In which case it can be written like this: a += b;