C # delegation and events

Source: Internet
Author: User
Tags types of functions

Entrusted with the flexibility of C # operation functions, we can use delegation to operate functions like operation variables. In fact, this function is not the first in C, as early as the C ++ era, there was a function pointer. In my opinion, delegation is the function pointer of C #. First, I would like to briefly introduce the basic knowledge of delegation:

Definition of delegation
The delegate declaration prototype is
Delegate <function return type> <delegate Name> (<function parameter>)
Example: public delegate void CheckDelegate (int number); // defines a delegate CheckDelegate. It can register a function that returns the void type and has an int as a parameter.
In this way, a delegate is defined, but the delegate is in. net is equivalent to declaring a class (This will be discussed in the code below). If the class is not instantiated as an object, many functions cannot be used, and so can the delegate.

Delegation instantiation
The prototype of delegation instantiation is
<Delegate type> <instantiate Name> = new <delegate type> (<register function>)
Example:CheckDelegate_ CheckDelegate = newCheckDelegate (CheckMod); // use the CheckMod function to instantiate the precedingCheckDelegateDelegate to _ checkDelegate
In. net 2.0, you can directly instantiate the delegate using the matching function:
<Delegate type> <instantiate name >=< register function>
Example:CheckDelegate_ CheckDelegate =CheckMod; // use the function CheckMod to instantiate the aboveCheckDelegateDelegate to _ checkDelegate
Now we can use the delegate just like using the function. In the above example, execute _ checkDelegate ()It is equivalent to executingCheckMod (), The most important thing is the current functionCheckMod is equivalent to a variable, which can be passed to otherCheckDelegate references objects and can be passed to other functions as function parameters or return types of functions.

Delegated Multicasting
When instantiating the delegate above, we can see that a matching function must be registered to the delegate to instantiate a delegate object. However, an instantiated delegate can register a function and multiple functions, after registering multiple functions, each registered function is executed in sequence when the delegate is executed.
Prototype of the function registration delegate:
<Delegate type> <instantiate Name> + = new <CommissionType> (<Register a function>)
Example:CheckDelegate_ CheckDelegate = newCheckDelegate (CheckMod); // call the FunctionCheckModRegister with the delegated instance_ CheckDelegateUpper
In. net 2.0, you can directly register the matching functionInstantiation delegate:
<Delegate type> <realFor example, not a real name> + =<Register a function>
Example:CheckDelegate_ CheckDelegate + =CheckMod; // setFunctionCheckModRegister with the delegated instance_ CheckDelegateUpper
Then we can register multiple functions to the delegate:
Example: _ checkDelegate + =CheckPositive; // setFunctionCheckPositiveRegister with the delegated instance_ CheckDelegateUpper
_ CheckDelegate (); // The instance that executes this delegate will first executeCheckMod () and then executeCheckPositive()

In fact, when the + = symbol is used, it will be determined
If the delegate has not been instantiated at this time (the delegate instance is null), it will automatically use the + = function on the right to instantiate the delegate.
If the delegate has been instantiated at this time, it will only register the function + = on the right to the delegate instance.
In addition, if you assign a value to the = number for the delegated instance that has registered the function, it is equivalent to re-instantiating the delegate, the previously registered functions and delegated instances do not have any relationships. This will be discussed in the following examples!

Of course, there are + = register the function to the delegate, and there are also-= unregister
Example:_ CheckDelegate-=NewCheckDelegate (CheckPositive); // releaseCheckPositivePair_ CheckDelegate.
_ CheckDelegate-=CheckPositive; //. net 2.0 can be removed in this way

C # events
After understanding the delegate, you can talk about the event. C # What is the event?
<

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.