When I first started to learn C #, I read the basic syntax and something. I wrote a demo and understood it. When I went back to use it, I started to use it. I had read a lot about this delegation, I never know when and where he will play a role! Remember only one keyword --"Delegate"And concept-"Is a reference type for methods with specific parameter lists and return types.".
It's hard to crack !!!!!!!!!!
I think about it recently. I want to write down my new understanding .....
Basic
Keywords: Delegate
Statement Syntax:
Public Delegate return value type delegate name (parameter list );
Instantiation: Delegate name Instance name;
Personal Understanding: "delegation" is entrusted to others as its name implies. In a language environment, it is entrusted to an object. To put it bluntly, authorization is used. I gave the bank card to a and told him my password. I entrusted him to help me get the money (of course, I trust it very much, otherwise there is a risk ), A has the right to get the money from my bank card.
In the program, it is a truth.
Instance demo
Demonstration purpose: Use a delegate to produce letters. Obtain the existing value from Textbox, specify the delegate binding method, and add it to ListBox.
Open vs2013 to create a winform project.
Create a new capital class, create only one method in the class, and convert the string to uppercase letters. The Code is as follows:
public class Capital { public string fixText(string s) { return s.ToUpper(); } }
Create a lower class, create only one method in the class, and convert the string to a letter. The Code is as follows:
public class Lower { public static string fixText(string s) { return s.ToLower(); } }
Life delegate and instantiate
private delegate string ftxDelegate(string s); ftxDelegate ftx;
Some code is omitted. The execution result is as follows.
The complete DEMO code is pushed here.
Access Password 799c