A small example using func Delegation
// Create an integer array int [] intarray = new int [] {0, 1, 2, 3}; // declare the func delegate to determine whether it is an odd number of func <int, bool> isodd = I => (I & 1) = 1); // run the query operation. Do not forget to have the "latency feature" ienumerable <int> items = intarray. where (isodd); // display the result foreach (INT item in items) console. writeline (item );
ProgramAs I wish to find out what are the odd numbers in the intarray, But what steps are actually saved by the func delegate behind it? I first studied the delegation in the LINQ query.
The where query prototype in LINQ.
An example is provided in my blog about lambda expressions. the example describes a situation where an int array needs to be identified, such as an odd or even number. the following describes the complete useful parts of this example.Code:
Using system; using system. collections; namespace linqtest {class program {public class commom {// name a delegate method Public Delegate bool intfilter (int I); // filter out the int that meets the requirements of the delegate method, returns an int [] public static int [] filterarrayofints (INT [] ints, intfilter filter) {arraylist alist = new arraylist (); foreach (int I in ints) if (filter (I) alist. add (I); Return (INT []) alist. toarray (typeof (INT) ;}// you can customize the filtering method Publ as needed. IC class myintfilter {// custom Filtering Method 1: Check whether it is an odd public static bool isodd (int I) {return (I & 1) = 1 );} // self-defined Filtering Method 2: Check whether it is an even public static bool iseven (int I) {return (I & 1 )! = 1 );}//... you can also define other filtering methods as needed} static void main (string [] ARGs) {int [] Nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // returns an odd number of int [] oddnums = commom. filterarrayofints (Nums, I => (I & 1) = 1); foreach (int I in oddnums) console. write (I + "");}}}
In this example, we define a commom class and filter out the required filterarrayofints method. If we use the extension method to extend filterarrayofints to the int [] type, it is interesting to change the filterarrayofints method name to where, so the code that filters out surprising numbers is like this:
Int [] Nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // filter the odd number int [] oddnums = nums. where (I => (I & 1) = 1 ));
Is it very similar to where in LINQ? That's the truth.
Function of func Delegation
<LINQ technical details> this is the case:This prevents developers from explicitly declaring the delegate type..
What does it mean? The above code is still useful and can be understood in this way. In the above Code, the myintfilter class defines many methods to filter certain conditional integers. If there is a method, there will be a method return type, if there is a parameter in the method, there will be another parameter type. as follows:
// Custom Filtering Method 1: Check whether it is an odd public static bool isodd (int I) {return (I & 1) = 1 );} // self-defined Filtering Method 2: Check whether it is an even public static bool iseven (int I) {return (I & 1 )! = 1 );}
The func delegate makes the code that defines these methods easy. If the above two methods are used, the func delegate only needs to write as follows:
Func <int, bool> isodd = I => (I & 1) = 1); func <int, bool> iseven = I => (I & 1 )! = 1 );
In this way, the writing method is relatively simple, and the meaning of the Code is clear at a glance.