C # Delegate, event, anonymous delegate,

Source: Internet
Author: User

C # Delegate, event, anonymous delegate,

As a beginner, writing down is used as your own learning notes, hoping to quickly find a way to solve problems in the future.

It would be better if I could help new people like me. If there is anything wrong or something that can be improved, I hope you can point it out. Thank you!

1. Delegate

Baidu: The delegate is a class, which defines the type of the method so thatPass the method as a parameter of another methodIn this way, the method is dynamically assigned to parameters, which can avoid the extensive use of If-Else (Switch) statements in the program, and make the program more scalable. (Baidu encyclopedia)

 

First, write a simple delegate:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApplication2 {class Program {// declares that there is no parameter and no value is returned for the delegate public delegate void MyDelegate (); // The delegate object static MyDelegate myDelegate; public static void Show () {Console. writeLine ("my proxy");} static void Main (string [] args) {myDelegate = new MyDelegate (Show ); // instantiate the delegate to delegate myDelegate to do the Show thing myDelegate (); // call the myDelegate Console. readKey ();}}}

 

Change the preceding delegate with parameters and return values.

Class Program {// declare a delegate with parameters and return values public delegate string MyDelegate (string data); // delegate object static MyDelegate myDelegate; public static string Show (string data) {return data;} static void Main (string [] args) {myDelegate = new MyDelegate (Show); // instantiate the delegate to delegate myDelegate to do Show operations Console. writeLine (myDelegate ("delegate Parameters"); // The Console parameter must be included during the call. readKey ();}}

 

Now let's write two methods

Class Program {// declare a delegate with parameters and return values public delegate void MyDelegate (); // delegate object static MyDelegate myDelegate; public static void ShowOne () {Console. writeLine ("delegate 1");} public static void ShowTwo () {Console. writeLine ("delegate 2 ");}}

 

Now we want to output one of the two, which can be changed to this. By the way, we will review the delegation with parameters.

Class Program {// declare the delegate public delegate void MyDelegate (string data) with parameters; // The delegate object static MyDelegate myDelegate; public static void Show (string data) {switch (data) {case "delegate 1": Console. writeLine ("delegate 1"); break; case "delegate 2": Console. writeLine ("delegate 2"); break;} static void Main (string [] args) {myDelegate = new MyDelegate (Show); myDelegate ("delegate 1 "); // when the parameter is called, the corresponding Console is output. readKey ();}}

What is entrusted? No. 1? entrusted? No. 2. This is a good note. If there are too many options, it's not easy to remember. How can we make some improvements?

/// <Summary> /// 1 is Commission 1 2 is Commission 2 // </summary> enum DATA {zero, NumOne, NumTwo ,}; class Program {// declare the delegate public delegate void MyDelegate (DATA data) with parameters; // The delegate object static MyDelegate myDelegate; public static void Show (DATA data) {switch (data) {case DATA. numOne: Console. writeLine ("delegate 1"); break; case DATA. numTwo: Console. writeLine ("delegate 2"); break;} static void Main (string [] args) {myDelegate = new MyDelegate (Show); myDelegate (DATA. numTwo); Console. readKey ();}}

 

Are you sure you want to use it again soon? // <summary> /// </summary> This is a good thing to make it clearer when others use your code. what can be used to improve code readability?

 

2. Events

What should we do if we want two methods to output at the same time:

Class Program {// declare the delegate public delegate void MyDelegate () with parameters and return values; // The delegate object static event MyDelegate myDelegate; // This is different from the above public static void ShowOne () {Console. writeLine ("delegate 1");} public static void ShowTwo () {Console. writeLine ("delegate 2");} static void Main (string [] args) {myDelegate = new MyDelegate (ShowOne ); // first assign a method myDelegate + = new MyDelegate (ShowTwo) to your object; // Add a method myDelegate () to you; // call the Console. readKey ();}}

Check that the output calls one or both of them are displayed at the same time. Yes, this is the event)

 

The event can be added. Can it be reduced? Smart. You're not mistaken. You can check the result after the change.

Static void Main (string [] args) {myDelegate = new MyDelegate (ShowOne); // assign a method myDelegate + = new MyDelegate (ShowTwo) to your object first ); // Add a method myDelegate () to you; // call the Console. writeLine ("______________________________ I will separate ____________________"); myDelegate-= new MyDelegate (ShowOne); myDelegate (); // call the Console again after the subtraction. readKey ();}

 

 

3. Anonymous Method

After the delegation and events are completed, let's look at the special anonymous method.

Each time you instantiate a delegate, you need a method that has been written, but the anonymous method does not need

Class Program {// declare the delegate public delegate void MyDelegate (string data) with parameters; // The delegate object static MyDelegate myDelegate; static void Main (string [] args) {myDelegate = delegate (string data) // with the parameter {Console. writeLine (data) ;}; myDelegate ("anonymous method with Parameters"); Console. readKey ();}}

If the parameter is not included, you only need to remove it. After delegate, follow the parameters you need to use in {} And do not forget to include a semicolon.

 

4. Lambda expressions

If the anonymous method is not quick enough, try this.

Class Program {// declare the delegate public delegate void MyDelegate (string data) with parameters; // The delegate object static MyDelegate myDelegate; static void Main (string [] args) {myDelegate = (string data) => {Console. writeLine (data) ;}; myDelegate ("Lambda expressions with Parameters"); Console. readKey ();}}

Is there less code? Write the parameter in parentheses directly, and then use another {} to frame your operation. Also, there is a semicolon at the end.

 

5. Action

Let's take a look at its explanation.

There can be a lot of parameters, but there is no returned value to see how to use them.

Class Program {// Action static Action actionOne without parameters; // Action static Action with parameters <string> actionTwo; static void Main (string [] args) {actionOne = new Action (ShowOne); actionOne (); actionTwo = new Action <string> (ShowTwo); actionTwo ("Action ShowTwo"); Console. readKey ();} static void ShowOne () {Console. writeLine ("Action ShowOne \ n");} static void ShowTwo (string data) {Console. writeLine (data );}}

Try to output it.

 

6. Func

It is similar to Action, but it must contain the returned value.

The previous parameter is its parameter, and the last parameter is its return value.

// Action static Func without parameters <string> FuncOne; // Action static Func with parameters <string, string> FuncTwo; static void Main (string [] args) {FuncOne = new Func <string> (ShowOne); Console. writeLine (ShowOne (); FuncTwo = new Func <string, string> (ShowTwo); Console. writeLine (ShowTwo ("Func ShowTwo"); Console. readKey ();} static string ShowOne () {return "Func ShowOne \ n";} static string ShowTwo (string data) {return data ;}

Let's try to output it by yourself. After all, the program should be more practical.

Related Article

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.