Effective C # Use delegate to implement callback

Source: Internet
Author: User

Delegation is a new term in C # and is widely used. For example, events are the simplest and most direct examples of delegation.

First, let's talk about delegation. In fact, delegation is a function pointer to people who have used C or C ++, but most people who use C # have never used these two languages, therefore, we do not have a deep understanding of delegation. We can simply understand delegation, that is, "entrusting others to execute some operations", that is, to execute an operation, however, this operation process is unknown, but it is just delegated for you to execute it.


See the following example.

Public   Delegate   Void Hellohandler ( String MSG );
Private   Void Sayhello ( String Smsg)
{
MessageBox. Show (smsg );
}

// Delegate sample
Hellohandler hhello =   New Hellohandler (sayhello );
Hhello (
" Hello world! " );

 

The example is obvious. When executing hhello, you don't know what the specific process is, but just execute it.
Now let's talk about what the delegate can do. First, let's analyze the event, which is also the most useful place for delegation. Generally, an event handler function needs to be bound to the initialization of an event. When an event is triggered, the handler function will be called. That is to say, the event handler function can clearly understand that, this event is triggered. For example, if you click the button on the form, the click event is fed back to the form.


Obviously, a delegate is used to process information interaction between objects. In addition to events, most delegate functions are similar to this, so the advantage of this is to reduce the coupling between objects.

Second, the delegate function differs from the previous function pointer in that multiple delegate functions can be bound, for example:

 

Public   Delegate   Void Hellohandler ( String MSG );
Private   Void Writehello1 ( String Smsg)
{
Debug. writeline (
"Writehello1:" +Smsg );
}

Private   Void Writehello2 ( String Smsg)
{
Debug. writeline (
"Writehello2:" +Smsg );
}

// Delegate sample
Hellohandler hhello =   New Hellohandler (writehello1 );
Hhello
+ =   New Hellohandler (writehello2 );
Hhello (
" Hello world! " );

 

 

However, in this case, the functions bound to the delegate are executed in order, so there are two potential problems.


The first problem is that when an exception occurs during function execution, subsequent operations cannot be performed. In the preceding example, if an exception occurs in the "writehello1" function, "writehello2" cannot be executed.


Another problem is the return value of the delegate execution. When multiple functions are bound, the return value of the delegate execution is the return value after the last function is bound, therefore, it will be incorrect to judge by this value.


For multiple functions bound to the delegate, note that the exception should not be spread out, and the return value of the delegate type is "Void ".

 

Many people read the examples in the book and understand the significance of delegation, but they cannot combine it with practical applications. When using delegation, you must first understand several issues. After all the problems are clear, the delegated prototype will naturally come out.

The following issues need to be clearly analyzed.
Question 1: What is the transfer of information, whether the transmission time is fixed, whether one transmission or multiple transmission;
Question 2: Is it appropriate to use delegation;
Question 3: Which party is the call end of the delegate, and which party is the initial end of the delegate;
Question 4: Where is the delegate initialization suitable.

The first problem is the key. This first determines whether to use Delegation for implementation. Second, if you want to use delegation, You need to determine the delegate function type.

for Question 2, many people may be puzzled. Yes, delegation can reduce the coupling between types, but it is not the only way to do this. In many cases, using overload constructors can achieve this goal. Therefore, you cannot limit the problem to this. For example, many people have created a separate form to modify a record, which can be implemented by delegation here, but considering that datarow belongs to the reference type, this form is meaningless if it is out of the data record. Therefore, you can reload the constructor and pass the record to the form when initializing the form. The latter is relatively simpler and more straightforward.
with the analysis of the first two problems, many users reverse the order when writing the delegation, therefore, the effect of Program execution is not as expected. Pay special attention to this.
for the fourth problem, you can refer to the control event initialization section of the form. Code , that is, the delegate initialization adopts the proximity principle, but this is not the only place for initialization, so writing is only convenient to prevent missing writing.
well, I 'd like to say so much about delegation and hope to help you.

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.