Summary: A comprehensive understanding of C # Delegation

Source: Internet
Author: User
Tags instance method

You have to understand the delegate before you say the event.
delegate, the appearance of a function pointer in C + + is no different, but in essence you find that he is actually a class! That is to say, understanding the delegate
This two aspects to understand (alone from one aspect to understand the feeling is strange hehe!)

Understanding delegates:
What is a delegate? Delegates are type-safe in C # and 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 the delegate is a type, further saying that he is a class!
His goal is to pass the method as a parameter to other methods! Further, it is a method within which a call to another method is required.
(And there is a method inside the method), and there is a multiplicity of methods for this internal invocation. The best way to do this is to use the internal method as an external
The number of parameters of the method of the part is passed ~ ~

The form of a delegate:
How to declare a delegate: Delegate return value type delegate type name (parameter)
such as delegate void Deleprocess (string s);
Note that in addition to the previous delegate, the remainder is the same as declaring a function, but deleprocess is not a function name, but a delegate type name!

Use of Delegates:
Delegates are in C #, and you can subscribe to one or more function pointers that have the same signature method. So the use of delegates is about the same as the use of function pointers.
Think of it as a pointer and know how to use it ~ ~ But the delegate is not fully regarded as a function pointer (essentially a class), because the delegate takes more security into account,
It needs to store the details of a particular method. So, once a delegate is defined, a delegate instance needs to be created before it can be used (to store the details of a particular method)!
Suppose that there is a function: void Sayhi (String s); This coincides with delegate void Deleprocess (string s), which is the same as the signature of this delegate.
Then we start using the delegate:
Deleprocess Pro = new Deleprocess (sayhi);//Instantiate a delegate and initialize it with the Sayhi method.
deleprocess Pro = Sayhi; This is the shorthand form above, and the compiler helped us new! Seems to be directly associated with the method (looks like a pointer to the feeling hehe), in fact, or through the Commission!
Note that Sayhi and pro do not have a "bracket", which is called if the parenthesis is the method. OK, when the delegate points to the method succeeds.
Pro (s) and Sayhi (s), achieve the same goal.
The final purpose of the delegate is for the method as a 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);//pro is the same as the method.
}
This allows the Sayhi method to be passed as a parameter:
Greetpeople (Song,sayhi);
Excerpt from "C # Advanced Programming"
One of the characteristics of delegates is that their type is secure, ensuring that the called method signature is correct.
But interestingly, they don't care what type of object The method is called, even if the method is not considered
is a static method, or an instance method.
Tips:
An instance of a given delegate can represent an instance method or a static method on any object of any type-as long as the method
Signature is matched to the signature of the delegate.
//-------------------------------------------------------------------------------------------------------------
Example of delegate 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 ("After Treatment");
foreach (string s in values)
{
Console.WriteLine (s);
}
}
Write functions, uppercase, lowercase, and quotation marks
Further experience: the difference between a function and a direct call: a delegate can point to any function, even if it is not defined before, but not by the use of those types.

Summary: A comprehensive understanding of C # Delegation

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.