Ashamed to say, in the course of the university, unexpectedly did not hear the name of the delegate. So today I'm going to take you through the delegates and the events.
Let's look at the Commission first.
I mainly explain in the following aspects
1, why use the delegate 2. What is a delegate 3. How delegates Use
Why use a delegate?
Delegates are a very important concept in C #, and using delegates enables programmers to encapsulate method references within a delegate object. You can then pass the delegate object to code that can invoke the referenced method without having to know at compile time which method will be invoked. Unlike function pointers in C or C + +, delegates are object-oriented and type-safe.
What is a delegate?
A delegate is a type of reference method, and once a method is assigned to a delegate, the delegate behaves the same way as the method, and the delegate method uses the same parameters and return values as other methods.
How to use a delegate
Let's put aside the boring concept, to see how a few living examples of how to use the commissioned it!
Case 1: Chinese and English say hello
Analysis: 1. First of all, we need to write a Chinese way to say hello and a British way to say hello.
2. Write a greet method, the greeting method as a parameter, to achieve the greetings of the people of various countries.
3. The use of delegates to achieve the greetings function of the people of various countries
Using delegates is divided into three steps: 1, define delegate 2. Declare delegate variable 3. Using delegates
Copy Code code as follows:
1. Define the delegate
public delegate void Greetdelegate (string name);
Class Program
{
static void Main (string[] args)
{
2. Declaring a delegate variable
Greetdelegate ddelegate = new Greetdelegate (chinesegreeting);
3. Using a delegate
Ddelegate ("Bruce Lee");
Console.readkey ();
}
Chinese Greetings method
public static void Chinesegreeting (string name)
{
Console.WriteLine ("Good Morning!") "+ name);
}
The English way of saying hello
public static void Englishgreeting (string name)
{
Console.WriteLine ("morning!" + name);
}
public static void Greet (string name, Greetdelegate makegreet)
{
Makegreet (name);
}
}
In order to deepen our understanding of the Commission, we will write a similar example.
Case 2: Translation cases: Translation of English into Chinese and Korean by delegation
Copy Code code as follows:
01. Declare a Delegate class
public delegate void Mytransdelegate (string words);
Class Program
{
static void Main (string[] args)
{
Translation methods
Call Method One
2. Declaration of Commission
Mytransdelegate del=new mytransdelegate (TRANSTOCN);
3. Invoke delegate
Del ("AA");
Calling method Two
Translate ("AA", TRANSTOCN);
Calling mode three
Mytransdelegate del = TRANSTOCN;
Del ("AA");
Console.readkey ();
}
static void Translate (String Str,mytransdelegate del)
{
Del (str);
}
static void Transtocn (String str)
{
Console.WriteLine ("translated into Chinese!") ");
}
static void Transtokorean (String str)
{
Console.WriteLine ("translated into Korean!") ");
}
}
In this case, we can see that there are more than one way to use a delegate, regardless of the way it is used, and ultimately the same way. By decompile we can prove that:
As shown in the following illustration I see method two code by decompile:
The following is the code for the Decompile method three
With the knowledge stored above, let's look at one more case:
Case 3: Passing a value from a subform to a parent form by delegating
If the figure has a parent form and a subform two form, now I want to implement the pop-up subform when I click the button in the parent form (this is super simple, of course), after the subform is popped, enter the text box in the subform, click the Close button to close the subform, and display the contents entered in the subform in the parent form's text box.
The code in the subform is as follows:
Copy Code code as follows:
Defining delegates
public delegate void Words (string Words);
Declaring a delegate variable
Public Words Words;
private void Button1_Click (object sender, EventArgs e)
{
Words (txtchild. Text);
This. Close ();
}
Code in the parent form:
private void Btnopen_click (object sender, EventArgs e)
{
Child child = new Child ();
Child.words = Getwords;
Child. Show ();
}
public void Getwords (String str)
{
Txtmain.text = str;
}
Let's analyze the two broken codes:
First, we want to update the data in the parent form text box when the subform closes. But we know very well that the space between forms is not directly accessible to each other, then we need to find an intermediary to help us implement the parent form data Update function. We then defined a delegate words with a parameter with no return value, and defined a method getwords the condition of the delegate in the parent form. At the same time, a delegate variable words is declared in the subform, and the delegate variable is invoked in the Close button.
But in the subform we don't have a subform delegate variable assigned to it, and the real assignment is placed in the main form. This enables you to invoke the parent form in a subform, which enables you to update the contents of the parent form text box.
Do not know, read the above 3 cases, we have a relatively clear understanding of the delegation, if this article can help you in the lost, it is better.