C # Part 5 of syntactic sugar: generic delegation-Action & lt; T & gt ;,

Source: Internet
Author: User

C # Part 5 of syntactic sugar: generic delegation-Action <T>,

I did not share my article yesterday because of my work (in fact, my personal inertia), and many garden friends also put forward their report opinions and guidance over the past few days, once again, I would like to thank these brothers for taking care of me and support. In this classified article, I wanted to share my learning experience and grammar used in my work with you, I hope it can help, but I am also a cainiao, not a great god, so there are also deficiencies in learning and areas that have not been scanned, for the syntax sugar problem of the last extension method, the extension method is indeed available at the beginning of 3.0. It may be that I have not used it before, and there is no lack of query. If you want to bring inconvenience to everyone, please forgive me. Now I have changed the title to C # syntactic sugar. I apologize for your support! Now let's go to the topic.

Action <T>Generic Delegation: The delegate transmits methods as parameters without explicitly declaring custom delegates. The encapsulated method must correspond to the method signature defined by the delegate. That is to say, the encapsulated method must have a parameter passed to it through a value and cannot return a value.

Generally, this method is used to execute an operation.

Let's start with this concept. Let's take a look at the example. Let's write a delegate by ourselves:

1 public static class generic delegate 2 3 {4 5 public static void print Student information (Student stu) 6 7 {8 9 Console. writeLine ("student ID:" + stu. stuNum + "name:" + stu. stuName + "Age:" + stu. age); 10 11} 12 13 public static void print instructor information (Teacher teach) 14 15 {16 17 Console. writeLine ("employee ID:" + teach. teachNum + "name:" + teach. teachName + "Age:" + teach. age); 18 19} 20 21 public delegate void PrintMsg <T> (T t); 22 23 public static void Teach <T> (this List <T> tList, printMsg <T> pt) 24 25 {26 27 foreach (T t in tList) 28 29 {30 31 pt (t ); 32 33} 34 35} 36 37} 38 39 public class Student40 41 {42 43 public string StuNum {get; set;} 44 45 public string StuName {get; set ;} 46 47 public int Age {get; set;} 48 49} 50 51 public class Teacher52 53 {54 55 public string TeachNum {get; set;} 56 57 public string TeachName {get; set;} 58 59 public int Age {get; set;} 60 61}View Code

The above is the implementation of generic delegation written by myself. Let's look at the call code:

1 List <Student> students = new List <Student> (); 2 3 students. add (new Student {StuNum = "001", StuName = "zhangsan", Age = 20}); 4 5 students. add (new Student {StuNum = "002", StuName = "lisi", Age = 21}); 6 7 students. add (new Student {StuNum = "003", StuName = "wangwu", Age = 22}); 8 9 List <Teacher> teacher = new List <Teacher> (); 10 11 teacher. add (new Teacher {TeachNum = "004", TeachName = "teacher1", Age = 50}); 12 13 teacher. add (new Teacher {TeachNum = "005", TeachName = "teacher", Age = 51}); 14 15 Teacher. add (new Teacher {TeachNum = "006", TeachName = "teacher3", Age = 52}); 16 17 students. teach (generic delegate. print student information); 18 19 teacher. teach (generic delegate. print instructor information); 20 21 Console. readLine ();View Code

The output code is as follows:

In the preceding example, the list <T> content is cyclically written and the code is output. The code is less, but the table shows generic delegation. After understanding the above example, you actually have a basic understanding of the implementation method of the built-in Delegate of Action <T>.

We transform the above code into a built-in delegate implementation. Here we use the ForEach () of List <T> for example:

1 public void ForEach (Action <T> action );View Code

This is the method defined by Microsoft. In fact, this is similar to the preceding Teach <T>.

1 List <Student> students = new List <Student> (); 2 3 students. add (new Student {StuNum = "001", StuName = "zhangsan", Age = 20}); 4 5 students. add (new Student {StuNum = "002", StuName = "lisi", Age = 21}); 6 7 students. add (new Student {StuNum = "003", StuName = "wangwu", Age = 22}); 8 9 List <Teacher> teacher = new List <Teacher> (); 10 11 teacher. add (new Teacher {TeachNum = "004", TeachName = "teacher1", Age = 50}); 12 13 teacher. add (new Teacher {TeachNum = "005", TeachName = "teacher", Age = 51}); 14 15 Teacher. add (new Teacher {TeachNum = "006", TeachName = "teacher3", Age = 52}); 16 17 students. teach (generic delegate. print student information); 18 19 teacher. teach (generic delegate. print instructor information); 20 21 Console. writeLine ("------------------------------- The following is the built-in delegated printing system"); 22 23 students. forEach (generic delegate. print student information); 24 25 teacher. forEach (generic delegate. print instructor information); 26 27 Console. readLine ();View Code

The above Code shows that the entire call method is similar, but the Teach method is replaced with the internal method ForEach.

In the above method, printing the student information and the instructor information are only used in this place. Therefore, we use the anonymous methods written in the previous article to print these two printing methods. The call method code is as follows:

1 teacher. forEach (new Action <Teacher> (delegate (Teacher t) 2 3 {4 5 Console. writeLine ("employee ID:" + t. teachNum + "name:" + t. teachName + "Age:" + t. age); 6 7}); 8 9 students. forEach (new Action <Student> (delegate (Student s) 10 11 {12 13 Console. writeLine ("employee ID:" + s. stuNum + "name:" + s. stuName + "Age:" + s. age); 14 15 }));View Code

The output code is as follows:

To simplify the code above:

1 teacher. forEach (delegate (Teacher t) 2 3 {4 5 Console. writeLine ("employee ID:" + t. teachNum + "name:" + t. teachName + "Age:" + t. age); 6 7}); 8 9 students. forEach (delegate (Student s) 10 11 {12 13 Console. writeLine ("employee ID:" + s. stuNum + "name:" + s. stuName + "Age:" + s. age); 14 15 });View Code

The output code is as follows:

In combination with the previous article, converting an anonymous method to a Lambda expression becomes the following code:

1 teacher. forEach (t => 2 3 {4 5 Console. writeLine ("employee ID:" + t. teachNum + "name:" + t. teachName + "Age:" + t. age); 6 7}); 8 9 students. forEach (s => 10 11 {12 13 Console. writeLine ("employee ID:" + s. stuNum + "name:" + s. stuName + "Age:" + s. age); 14 15 });View Code

The output code is as follows:

 

The above is a sharing of C # syntax sugar generic delegate Action <T>, hoping to help you learn.

Original article web site http://www.yaosutu.cn/archives/564


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.