Delegates and events

Source: Internet
Author: User

A delegate can be understood as a unique interface, that is, an interface that encapsulates only one method, when the method does not know the specific implementation and does not know which object is specifically implemented by the method. Need to be implemented by others.

The two are functionally the same!

The difference is:
1. The delegate is only a single method, and the interface can encapsulate multiple methods
2. The delegate only constrains the signature of the method, and the name of the interface constraint method

So from a polymorphic perspective, a delegate can be thought of as a lightweight interface.

Example 1:

Now I'm going to sort through a series of data, and the sorting algorithm may be complicated, I won't write it myself, I want to call the Array.Sort method, and Microsoft provides us with a quick sort algorithm.
But here's the problem--I'm going to implement custom collations, like strings, by default alphabetically, but now I want to sort this out:

Sort by string length, and then alphabetically, only if the length is not the same.

Obviously, Microsoft is not able to provide such a "personality" of the sorting method, that is not to say, we have to write our own fast sorting algorithm?
No need!
We only need to use a delegate to implement 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 do not need to know the logic of the fast sorting algorithm, I just need to tell the collation, I realized my personality sort.

Ask: How do you solve this problem without a delegate?

The simplest example of 2:

Array has a sort method, if you want to implement a custom sort, there are two overloads: delegates and interfaces

Delegate:  Public Static void Sort<t> (    t[] array,    Comparison<T> Comparison)
Interface:  Public Static void Sort<t> (    t[] array,    IComparer<T> comparer)

Example 2

Now I'm going to sort through a series of data, and the sorting algorithm may be complicated, I won't write it myself, I want to call the Array.Sort method, and Microsoft provides us with a quick sort algorithm.
But here's the problem--I'm going to implement custom collations, like strings, by default alphabetically, but now I want to sort this out:

Sort by string length, and then alphabetically, only if the length is not the same.

Obviously, Microsoft is not able to provide such a "personality" of the sorting method, that is not to say, we have to write our own fast sorting algorithm?
No need!
We only need to use a delegate to implement 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 do not need to know the logic of the fast sorting algorithm, I just need to tell the collation, I realized my personality sort.

Ask: How do you solve this problem without a delegate?

Here's a vernacular delegate and event

Http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1703763.html

Delegates and events

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.