About delegate in C #

Source: Internet
Author: User

Why use delegate?

 

The following is a helpful and understandable response from csdn.

As the landlord said, in some places, you can directly call the object method without entrusting it.
But sometimes this is not suitable.
For example, when writing this class, you cannot determine the method of the object to be called. For example, you can hook your own method on an object to Microsoft textbox.
. When writing Textbox, Microsoft simply won't know which method of the object to call when this event occurs. You only need to specify the method to be called and delegate
Event.

When Microsoft writes a textbox event, the only thing that can be determined is the event format, or the format of the method to be called for this event,
Similar to button#click (Object sender, enentargs E. Methods written according to this type can be mounted to this event and called when an event occurs.

If the format of the event method is specified, the method called when the event occurs is legal, and the method Type mismatch does not occur. This is the so-called type-safe delegation. C ++ uses
The void * function pointer does not check the Function Format for event processing. Therefore, C # invented the blame for entrusting the function to ensure that the format of the called function is correct.

This is only one of the message-driven implementations.

Another common method is to use interfaces. This is the Java event processing method. It specifies the format of the called method in the form of an interface. This method is more direct, simpler, and easier to understand.

The two methods have their own advantages and disadvantages and have their own use cases, but the effect is equivalent.

------------------------------------------------------------

On the 7th floor, we will talk about events. I will give you an example of delegation.
Now I want to sort a series of data.AlgorithmIt may be complicated. I won't write it myself. I want to call the array. sort method. Microsoft provides us with a fast sorting algorithm.
But there is a problem here -- I want to implement custom sorting rules. For example, for strings, the default sorting is in alphabetical order, but now I want to sort them like this:

Sort by string length, only when the length is different, then sort by letter.

Obviously, Microsoft cannot provide such a "personalized" sorting method. That is not to say, we must write a fast Sorting Algorithm ourselves?
No!
We only need to use the delegate to achieve this requirement:
String [] STRs = "I like C # very much". Split ();
Array. Sort (STRs, rule );
Int void rule (string first, string second)
{
Return first. Length = second. length? First. compareto (second): First. length. compareto (second. Length );
}

Obviously, I don't need to know the logic of the fast sorting algorithm. I just need to tell the sorting rules to achieve my personalized sorting.

Q: How do you solve this problem if no Commission is provided?

-------------------------------------------------------------------

It cannot be solved either. Define an interface, and then inherit the interface to write a class, and write the sorting rule in the interface function.
Then upload a class instance to it, and sort the instance internally as the interface for processing. This is the solution for Java.
But...
Obviously, for a new class, I prefer to delegate a function to solve the problem. How many times should I press the keyboard to write a new class...
In addition, in C #3.0, I am too lazy to write new functions. A Lambda expression can be used.

 

 

Original post:

Http://topic.csdn.net/u/20090102/23/b26f5193-8eb1-4c57-a129-123ddbbe85a4.html

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.