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