Discussion on delegated multicast Delegation

Source: Internet
Author: User

+ = Operator
-= Operator

I learned about multicast delegation. Now let's talk about the + = Operator and the-= Operator.

In our previous studies, we know that the + = Operator is used for auto-increment operations, but in the delegate, the + = operator has a new meaning.
First look at an instance:
First define a delegate type
Public delegate void del (int a, int B );

Then define a delegate variable:
Del delegate;

Now, let's assume that we have the following methods to use this type of delegation:
1) void add (int a, int B)
{
Console. writeline (a + B );
}
2) void minus (int a, int B)
{
Console. writeline (a-B );
}
3) void ride (int a, int B)
{
Console. writeline (a * B );
}
4) void divide (int a, int B)
{
Console. writeline (a/B );
}
 
Now, we want to input two integers and calculate the result after their addition, subtraction, multiplication, division, and limit the use of the delegate output. You must think like this:
Public static void send (int a, int B, del handler)
{
Handler (a, B );
}
Public static void main (string [] args)
{
Del delegate1 = add;
Del delegate2 = minus;
Del delegate3 = ride;
Del delegate4 = divide;

Sand (1, 2, delegate1 );
Sand (1, 2, delegate2 );
Sand (1, 2, delegate3 );
Sand (1, 2, delegate4 );
Console. readkey ();
}

The above method is indeed feasible, but the mian method is too cumbersome to write, so the use of multicast delegation can greatly simplify the code and improve efficiency

Now, the main method can be rewritten as follows:
Public static void mian (string [] args)
{
Del delegatesand = add;
Delegatesand + = minus;
Delegatesand + = ride;
Delegatesand + = divide;

Sand (1, 2, delegatesand );

Console. readkey ();
}

In this way, the code will be simplified and less declarative variables will improve efficiency.

Maybe, when you see this piece of code, you will be surprised, + = won't assign a value to delegatesand again?

But I want to say that your worries are superfluous. In our previous understanding, the + = operator has the function of assigning values, but why is it the same as the above Code, this is different from your imagination. In fact, the + = Operator is overloaded, which is not generally understood as follows:
{
Int a = 0, B = 1;
A + = B; // At this time, a becomes 1
}

When a delegate is used, + = will be overloaded, and its function becomes to add a reference delegate method for the assigned delegate variable without affecting the assigned delegate variable.
That is to say, each time the + = Operator is used, the delegate variable will be added to a delegate method. For example, after the original delegate variable is assigned a value, the delegate variable is also used as the delegate for the two delegate methods at the same time, and they run in the same order as the order in which they are added.

Similarly, the-= Operator indicates that, when used by a delegate, it is used to subtract a delegate method from an existing delegate variable (assigned value ).

For example, modify the main method in the previous example.

Public static void mian (string [] args)
{
Del delegatesand = add;
Del delegatesand + = minus;
Del delegatesand + = ride;
Del delegatesand + = dividel;

Sand (1, 2, delegatesand );

Console. writeline ("---------------------");

Delegatesand-= add;

Sand (1, 2, delegatesand );

Console. readkey ();
}

Output result: 3
-1
2
0
-------------------
-1
2
0


The calculation result of the add () method is not found in the second result.

This is the role of the-= Operator.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.