C # No basic learning and understanding Commission

Source: Internet
Author: User

To put it bluntly, I have never heard of the name of delegation in university courses. So today I will discuss with you the delegation and events.

Let's first look at the delegation

I will mainly explain from the following aspects:

1. Why do I use a delegate? 2. What is a delegate? 3. How do I use a delegate?

Why use delegation?

Delegation is a very important concept in c #. With delegation, programmers can encapsulate method references in the delegate object. The delegate object can then be passed to the code that can call the referenced method, without knowing which method will be called during compilation. Unlike function pointers in C or C ++, delegation is object-oriented and type-safe.

What is delegation?

A delegate is a type of reference method. Once a delegate is assigned a method, the Delegate will have the same behavior as the method. The delegate method has parameters and return values like other methods.

How to Use Delegation

Let's leave the boring concept behind. Let's look at several examples of how to use delegation!

Case 1: Greetings from Chinese and British

Analysis: 1. First, we need to write a method of saying hello to the Chinese and a method of saying hello to the British.

2. Compile a Greet method and use the "hello" method as a parameter to implement the "hello" Method for people in different countries.

3. Use delegation to send greetings to people in different countries

There are three steps to use delegate: 1. Define delegate 2. Declare delegate variable 3. Use delegate

Copy codeThe Code is as follows: // 1. Define the delegate

Public delegate void GreetDelegate (string name );

Class Program

{

Static void Main (string [] args)

{

// 2. Declare the delegate variable

GreetDelegate dDelegate = new GreetDelegate (ChineseGreeting );

// 3. Use the delegate

DDelegate ("bruce lee ");

Console. ReadKey ();

}

// Chinese saying hello

Public static void ChineseGreeting (string name)

{

Console. WriteLine ("good morning! "+ Name );

}

// Method of saying hello to the British

Public static void EnglishGreeting (string name)

{

Console. WriteLine ("Morning! "+ Name );

}

Public static void Greet (string name, GreetDelegate makeGreet)

{

MakeGreet (name );

}

}

To deepen your understanding of delegation, let's write a similar example.

Case 2: Translation case: entrust the English Translation into Chinese and Korean

Copy codeThe Code is as follows: // 01. Declare a delegate class

Public delegate void MyTransDelegate (string words );

Class Program

{

Static void Main (string [] args)

{

// Translation method

// Call method 1

// 2. Declare Delegation

MyTransDelegate del = new MyTransDelegate (TransToCN );

// 3. Call the delegate

Del ("aa ");

// Call method 2

Translate ("aa", TransToCN );

// Call method 3

// 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 tranmo-orean (string str)

{

Console. WriteLine ("translated into Korean! ");

}

}

Through this case, we can see that there are more than one delegate method, no matter which method is used, it is eventually the same as the method. Through decompilation, we can prove that:

As shown in, I can see the code of method 2 through decompilation:

The code in method 3 after decompilation is as follows:

With the above knowledge storage, let's look at another case:

Case 3: Pass the values in the child form to the parent form through delegation

There are two forms: parent form and child form. Now I want to pop up the child form when I click the button in the parent form (of course this is super simple). After the child form pops up, enter content in the text box of the child form, click the close button, and close the child form. The content entered in the child form is displayed in the text box of the parent form.

The code in the child form is as follows:

Copy codeThe Code is as follows: // define the delegate

Public delegate void Words (string words );

// Declare the delegate variable

Public Words words;

Private void button#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 broken codes:

First, we want to update the data in the text box of the parent form when the child form is closed. But we know very clearly that the space between forms cannot be directly accessed from each other. In this case, we need to find an intermediary to help us update the data of the parent form. Therefore, we defined a delegate Words with a parameter without a return value, and defined a method in the parent form that meets the condition of the delegate. At the same time, a delegate variable word is declared in the subform, and the delegate variable is called in the close button.

However, in the child form, we do not assign values to the child form delegate variables. The true values are placed in the main form. In this way, the method of calling the parent form in the child form is implemented through delegation, and the content of the text box of the parent form is updated.

I don't know. I have read the above three cases. Do you have a clear understanding of the Delegation? It would be better if this article can help you in the Lost Path.

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.