C # Delegate

Source: Internet
Author: User

Delegates belong to the term in C # and are widely used, for example, events are the simplest and most straightforward examples of delegates.

So first of all to say what is a delegate, in fact, the delegate in the use of C or C + + people seem to be a function pointer, but most people who use C # have not used these two languages, so the understanding of the delegation is not very deep, A delegate can be simply literally understood as "delegating someone else to perform certain operations", which means performing an operation that is not known by itself, but entrusted to you to execute.

See the example below.

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, in the implementation of Hhello , do not know the specific process is what, just to implement it.

Now let's talk about what the delegate can do, so let's analyze the event first, which is the place where the Commission is most used. The initialization of a general event requires binding an event handler, and when the event is triggered, the handler is called, that is, the event handler can clearly know that the event is triggered. For example, clicking the Buttonon the form will give the click Event Feedback to the form. It is clear that delegates are handling information interactions between objects. In addition to events, the majority of the use of delegates is similar to this, so the advantage of using this is to reduce the coupling between objects.

The second delegate differs from the previous function pointer in that it is possible to bind multiple delegate functions, 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!");

In this case, however, there are two potential problems because the functions that are bound to the delegate are executed in order.

The first problem, when an exception occurs during a function execution, causes the subsequent failure to execute. In the example above, if anexception occurs in the "WriteHello1" function, "WriteHello2" cannot be executed.

Another problem is that the return value of the delegate execution, when bound to multiple functions, the return value of the delegate execution is the last binding function after the execution of the return value, then by this value to judge will be incorrect.

So for a delegate to bind multiple functions, be aware that the exception is not diffused, and the return value of the delegate type is "void".

How to use the delegate, many people read the book examples, but also know the meaning of the delegation, but it can not be combined with the actual application. Then in the use of the Commission, the first to understand a few questions, and so on, the problem is clear, the commissioned prototype will naturally come out.

There are a few things that need to be analyzed clearly.

Question one, what is the transmission of information, whether the timing is fixed, one pass or multiple passes;

Question two, whether the Commission is appropriate;

Question three, which party is the invocation end of the delegate, and which party is the initial end of the delegate;

In question four, the initialization of the delegate is put in a more appropriate place.

Question one is the key, which first determines whether to use a delegate, and secondly, if you want to use a delegate, you need to determine the type of the delegate function.

For question two, many people may be puzzled. Yes, a delegate can reduce the coupling between types, but this is not the only way to do this. In many cases, this can be achieved with overloaded constructors, so it is not limited to thinking about the problem. For example, many people have done a separate form to make changes to a single record, which can be implemented with a delegate, but given that the DataRow is a reference type, and that the form is out of the data record, it loses its meaning, so the constructor can be overloaded, and when the form is initialized, It's OK to pass the record to the form. Comparatively speaking, the latter would be simpler and more straightforward.

With the analysis of the first two problems, a lot of people in the writing of the delegation, will be the order of the reverse, so the effect of the program is not envisaged, this point to pay particular attention.

For the fourth question, you can use the form's control events to initialize part of the code, that is, the initialization of the delegate takes the nearest principle, but this is not the only place to initialize, so that writing is easy to prevent the omission of writing.

C # Delegate

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.