Conclusion: C # delegation's comprehensive understanding,

Source: Internet
Author: User

Conclusion: C # delegation's comprehensive understanding,

You must first understand the delegate before talking about the event.
Delegate, the appearance seems to be no different from the function pointer in C/C ++, but essentially you find that it is actually a class! That is to say, the understanding of delegation is from
Understanding these two aspects (it's strange to understand one aspect alone !)

Understanding delegation:
What is delegation? The delegate is type-safe in c #. You can subscribe to one or more function pointers with the same signature method.
Declaring a delegate is equivalent to declaring a function pointer type, and a delegate is a type. Further, it is a class!
The purpose is to pass methods as parameters of other methods! Further, a method needs to call another method internally.
(And there is another method inside the method), and there are various methods called internally. The best way is to take the internal method as an external
The parameter Number of the partial method is used to pass the parameter ~~

Form of delegation:
How to declare delegation: delegate return value type delegate type name (parameter)
For example, delegate void deleProcess (string s );
Note that except the preceding delegate, the remaining part is the same as a function, but deleProcess is not a function name, but a delegate type name!

Use of delegation:
The delegate is in c #. You can subscribe to one or more function pointers with the same signature method. Therefore, the use of delegation is similar to that of function pointers.
Think of it as a pointer and you will know how to use it ~~ However, the delegate cannot be completely regarded as a function pointer (essentially a class), because the delegate considers more security,
It needs to store details of specific methods. Therefore, after you define a delegate, you must create a delegate instance to use it (to store the details of a specific method )!
Assume there is a function: void SayHi (String s); this is exactly the same as delegate void deleProcess (string s); the signature of this delegate.
Then we start to use the delegate:
DeleProcess pro = new deleProcess (SayHi); // instantiate a delegate and initialize it using the SayHi method.
DeleProcess pro = SayHi; // This is the abbreviated form above. The Compiler helps us with new! It looks like it is directly associated with the method (it seems like a pointer), but it is still done through delegation!
Note that SayHi and pro do not contain "brackets". This should be the method call if brackets are attached. After the delegate points to the method successfully.
Pro (s) and SayHi (s) achieve the same purpose.
The final purpose of delegation is to use the method as the parameter of the method:
Viod GreetPeople (string name, deleProcess pro) // This GreetPeople method uses the delegate to take the method as a parameter!
{
Pro (name); // The method used by pro is the same ~~
}
In this way, the SayHi method can be passed as a parameter:
GreetPeople (song, SayHi );
// Excerpted from C # Advanced Programming
One feature of delegation is that their types are secure and the signature of the called method can be correct.
But interestingly, they do not care about the type of objects that call the method, or even ignore the method.
Static Method or instance method.
Tip:
An instance with a given delegate can represent the instance method or static method on any object of any type-as long as the method
The signature matches the signature of the delegate.
// Configure //-------------------------------------------------------------------------------------------------------------
// Example of delegated use ~~
Delegate string ProcessDelegate (string s );
Static void ProcessIntArray (ProcessDelegate p)
{
String [] values = new string [] {"AB", "Cd "};
For (int I = 0; I <values. Length; I ++)
{
Values [I] = p (values [I]);
}
Console. WriteLine ("processed ");
Foreach (string s in values)
{
Console. WriteLine (s );
}
}
Write Functions in upper case, lower case, and quotation marks
Further understanding: the difference from calling a function directly: A delegate can point to any function, even if it is not previously defined, but not limited to those.

Related Article

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.