Common system built-in delegates in C #
Action class, Func class, predicate<t>, Comparison<t> delegate
- Delegate for Action class
- The action delegate encapsulates a method that does not have parameters and does not return a value
The Action<t> delegate encapsulates a method that has only one parameter and does not return a value
- The Action<t1,t2> delegate encapsulates a method that has two parameters and does not return a value
Static voidMain (string[] args) {#regionAction<t> Delegate Example//Requirements: Print out the elements of the integer collection listlist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a action<t> delegate instanceaction<int> CONCAT1 =Delegate(inti) {Console.WriteLine (i);}; List. ForEach (CONCAT1); //Assigning a lambda expression to an action<t> delegate instanceaction<int> concat2 = (i =Console.WriteLine (i)); List. ForEach (CONCAT2); Console.readkey (); #endregion }
- Delegate for the Func class
- 1.Func (TResult) delegate encapsulates a method that encapsulates a type value that does not have a parameter but returns the TResult parameter
- The Func (T,tresult) delegate encapsulates a method that has a parameter and returns the type value specified by the TResult parameter
- The Func (T1,t2,tresult) delegate encapsulates a method that has two parameters and returns the type value specified by the TResult parameter
Static voidMain (string[] args) { #regionFunc<t,tresult> Delegate Example//requirement: Finds a new collection of all elements greater than 3 in the list of integral types and prints out the collection elementslist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a func<t,tresult> delegate instancefunc<int,BOOL> CONCAT1 =Delegate(inti) {returni >3; }; varNewlist1 =list. Where (CONCAT1). ToList (); //Assigning a LAMBDA expression to an func<t,tresult> delegate instancefunc<int,BOOL> concat2 = i = i >3; varNewlist2 =list. Where (CONCAT2). ToList (); Newlist1. ForEach (i=Console.WriteLine (i.ToString ())); Newlist2. ForEach (i=Console.WriteLine (i.ToString ())); Console.readkey (); #endregion}
Represents a method that defines a set of conditions and determines whether the specified object conforms to these conditions
Static voidMain (string[] args) { #regionPredicate<t> Delegate Example//requirement: Finds a new collection of all elements greater than 3 in the list of integral types and prints out the collection elementslist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a predicate<t> delegate instancepredicate<int> CONCAT1 =Delegate(inti) {returni >3; }; varNewlist1 =list. FindAll (CONCAT1); //Assigning a lambda expression to an predicate<t> delegate instancepredicate<int> CONCAT2 = (c = c >3); varNewlist2 =list. FindAll (CONCAT2); Newlist1. ForEach (i=Console.WriteLine (i)); Newlist2. ForEach (i=Console.WriteLine (i)); Console.readkey (); #endregion}
Represents a method that compares two objects of the same type
Static voidMain (string[] args) { #regionComparison<t> Delegate Example//Requirement: Prints all elements in the list of integral types in reverse orderlist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a comparison<t> delegate instancecomparison<int> CONCAT1 =Delegate(intIintj) {returnJ-i;}; //Assigning a lambda expression to an comparison<t> delegate instancecomparison<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}
Common in C # system built-in delegate usage detailed (transcription)