C # commonly used built-in system delegation,

Source: Internet
Author: User

C # commonly used built-in system delegation,

In the Common Language Runtime (CLR) environment, the system has built some common delegation for us, including the delegate of the Action class, the delegate of the Func class, the delegate of the Predicate <T>, and the delegate of the Comparison <T> class. The namespace of these delegates is System and the Assembly is mscorlib. dll. Today I will talk about how to use these delegates.

Just as we have defined, to implement some functions, we can directly use the built-in system delegate to instantiate them, instead of explicitly defining a new delegate and assigning the naming method to the delegate. For example:

Public static void Test () {Console. writeLine ("Just For Test");} static void Main (string [] args) {Action a = new Action (Test); // instantiate an Action delegate directly, you don't have to decide to delegate ();}

As long as you know what the system's built-in delegation is doing, what parameters are passed, and what values are returned, you can call them on your own like the above example. The following is my summary of these four types of delegation, along with examples that combine anonymous methods and Lambda expressions. The results of the two methods are the same.

1. Action-class Delegation

1. The Action delegate encapsulates a method. This method does not have parameters and does not return values.

2. Action <T> the delegate encapsulates a method. This method has only one parameter and does not return a value.

3. Action <T1, T2> delegates to encapsulate a method that has two parameters and does not return values.

...... ......

17. action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> encapsulate a method, this method has 16 parameters and does not return values.

The following uses the Action <T> delegate as an example to demonstrate how to use the delegate of the Action class, which is only different in the number of parameters.

Static void Main (string [] args) {# region Action <T> delegate example // requirement: print the list of elements in the List of integer sets <int> list = new List <int> () {1, 2, 3, 4, 5 }; // assign the anonymous method to Action <T> delegate instance Action <int> concat1 = delegate (int I) {Console. writeLine (I) ;}; list. forEach (concat1); // assign the lambda expression to Action <T> delegate instance Action <int> concat2 = (I => Console. writeLine (I); list. forEach (concat2); Console. readKey (); # endregion}

Summary:

The delegate of the Action class can contain at least 0 parameters and a maximum of 16 parameters. The parameter types are invert and do not return values.

Ii. Func class Delegation

1. the Func (TResult) delegate encapsulates an encapsulation that does not have parameters but returnsTResultMethod of the type value specified by the Parameter

2. the Func (T, TResult) delegate encapsulates a request with a parameter and returnsTResultMethod of the type value specified by the Parameter

3. the Func (T1, T2, TResult) delegate encapsulates a request with two parameters and returnsTResult Method of the type value specified by the Parameter

...... ......

17. func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> the delegate encapsulates a method, which has 16 parameters and returnsTResultValue of the type specified by the Parameter

The following uses the Func <T, TResult> delegate as an example to demonstrate how to use the delegate of the Func class, which is only different in the number of parameters.

Static void Main (string [] args) {# region Func <T, TResult> delegate example // requirement: Search for a New set composed of all elements larger than 3 in the list of integer sets, and print the set element List <int> list = new List <int> () {1, 2, 3, 4, 5 }; // allocate anonymous methods to Func <T, TResult> delegate instance Func <int, bool> concat1 = delegate (int I) {return I> 3 ;}; var newlist1 = list. where (concat1 ). toList (); // assign the Lambda expression to Func <T, TResult> delegate instance Func <int, bool> concat2 = I => I> 3; var newlist2 = list. where (concat2 ). toList (); newlist1.ForEach (I => Console. writeLine (I. toString (); newlist2.ForEach (I => Console. writeLine (I. toString (); Console. readKey (); # endregion}

Summary:

Func-type delegation can input at least one generic parameter (in, inverter) and up to 16 generic parameters (in, inverter, the input output generic parameter (out, covariant) has only one and only one type, which is the return value type of the method encapsulated by this delegate.

Iii. Delegate Predicate <T>

Defines a set of conditions and determines whether the specified object meets these conditions.

The following is an example of Predicate <T> delegation:

Static void Main (string [] args) {# region Predicate <T> delegate example // requirement: Find a new set composed of all elements larger than 3 in the list of integer sets, and print the set element List <int> list = new List <int> () {1, 2, 3, 4, 5 }; // assign the anonymous method to the Predicate <T> delegate instance Predicate <int> concat1 = delegate (int I) {return I> 3 ;}; var newlist1 = list. findAll (concat1); // assign the lambda expression to the Predicate <T> delegate instance Predicate <int> concat2 = (c => c> 3); var newlist2 = list. findAll (concat2); newlist1.ForEach (I => Console. writeLine (I); newlist2.ForEach (I => Console. writeLine (I ));
Console. ReadKey (); # endregion}

Summary:

The Predicate <T> delegate encapsulates a method. This method is used to input a type parameter. This parameter refers to the type of the object to be compared. This type parameter is inverter, receive a parameter at the same time (this parameter is the object that needs to be compared according to the conditions defined in the method represented by this delegate, and the parameter type is the type of the input type parameter ), this method always returns a value of the bool type. True if the object meets the conditions defined in the methods represented by this delegate; otherwise, false.

Iv. Comparison <T> delegate

The method used to compare two objects of the same type.

The following is an example of Comparison <T> delegation:

Static void Main (string [] args) {# region Comparison <T> delegate example // requirement: print out all elements in the list of integer sets in reverse order. List <int> list = new List <int> () {1, 2, 3, 4, 5 }; // assign the anonymous method to Comparison <T> delegate instance Comparison <int> concat1 = delegate (int I, int j) {return j-I ;}; // assign the lambda expression to Comparison <T> delegate instance Comparison <int> concat2 = (I, j) => j-I; list. sort (concat1); list. forEach (c => Console. writeLine (c. toString (); list. sort (concat2); list. forEach (c => Console. writeLine (c. toString ()));
Console. ReadKey (); # endregion}

Summary:

Comparison <T> delegates to encapsulate a method that passes in a type parameter, which refers to the type of the object to be compared. This type parameter is inverter, receive two parameters of the same type at the same time (these two parameters are the two objects to be compared, the parameter type is the type of the input type parameter), always return the value of the int type, it is a signed integer that indicates the relative values of x and y, as shown in the following table.

Value Description
Less than 0 X less than y
0 X equals to y
Greater than 0 X is greater than y

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.