C # Delegate 0 basic understanding (RPM)
1, why use Delegate 2. What is a delegate 3. How the delegate is used
Why use a delegate?
Delegates are a very important concept in C #, and using delegates allows programmers to encapsulate a method reference within a delegate object. You can then pass the delegate object to the code that invokes the referenced method without knowing at compile time which method will be called. Unlike function pointers in C or C + +, delegates are object-oriented and type-safe.
What is a delegate?
A delegate is a type that refers to a method, and once a delegate is assigned a method, the delegate behaves the same as the method, and the delegate method uses the same arguments and return values as the other methods.
How to use Delegates
Let's take a look at a few living examples of how to use a delegate.
Case 1: Chinese and English say hello
Analysis: 1. First we need to write a way for Chinese to say hello to a British person.
2. Write a greet method, the greeting method as a parameter, to achieve the people of different countries say hello method.
3. Use the delegation to realize the function of saying hello to people in different countries
The use of delegates is divided into three steps: 1, defining delegates 2. Declaring a delegate variable 3. Using delegates
1. Defining delegates
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 delegates
Ddelegate ("Bruce Lee");
Console.readkey ();
}
How to say hello to Chinese
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);
}
}
To deepen our understanding of the Commission, we will write a similar example.
Case 2: Translating cases: Translating English into Chinese and Korean through delegation
01. Declaring a Delegate class
public delegate void Mytransdelegate (string words);
Class Program
{
static void Main (string[] args)
{
Translation methods
Call Method One
2. Declaration of Delegation
Mytransdelegate del=new mytransdelegate (TRANSTOCN);
3. Invoking delegates
Del ("AA");
Call Method Two
Translate ("AA", TRANSTOCN);
Call 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 anti-compilation We can prove that:
As shown in my anti-compilation See method two code:
The following is the code for post-compilation method three
With the above knowledge store, let's look at one more case:
Case 3: Passing a value from a subform to a parent form through a delegate
There are two forms of the parent form and the subform, and now I want to implement when the subform pops up when you click the button in the parent form (which is super simple, of course), after the subform pops up, type in the subform's text box, click the Close button, the subform closes, and the content entered in the subform is displayed in the parent form's
The code in the subform is 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 above two-break code:
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 functionality. We define a delegate words with a parameter that does not return a value, and define a method getwords a condition that conforms to the delegate in the parent form. At the same time, a delegate variable words is declared in the subform, and the delegate variable is called in the Close button.
But in the subform we don't have a delegate variable assigned to the subform, and the true assignment is placed in the main form. This enables the method to invoke the parent form in the subform by means of a delegate, thereby updating the contents of the parent form's text box.
Do not know, read the above 3 cases, we have a relatively clear understanding of the Commission, if this article can help you in the lost, it is better.
C # Delegate 0 basic understanding