Delegate is a class that defines the type of method, such a method as a parameter may be passed to another process. Event is a special delegate.
1. The commission's statement
(1). delegate
delegate to us a common statement
Delegate parameter at least 0, up to 32 parameters, no return value, the return value can also be specified type.
Example: public delegate int MethodtDelegate (int x, int y); represents two parameters, and returns an int.
(2). Action
Action generic delegates is no return value.
Action means no parameters and returns no value commissioned
Action <int, string> delegate incoming parameters expressed int, string no return value
Action <int, string, bool> expressed delegate incoming parameters int, string, bool no return value
Action <int, int, int, int> 4 represents an incoming int argument, no return value delegate
Action parameter of at least 0, up to 16 parameters, no return value.
Example:
public void Test<T>(Action<T> action,T p) { action(p); }
(3). Func
Func generic delegates have returned values
Func <int> represents no arguments, returns an int value entrusted
Func <object, string, int> parameter means that the incoming object, string returns an int value entrusted
Func <object, string, int> parameter means that the incoming object, string returns an int value entrusted
Func <T1, T2,, T3, int> parameter returns the delegation means that the incoming int value is T1, T2,, T3 (generic)
Func parameter of at least 0, up to 16 parameters, generic return by return. There must be a return value, not void
Example:
public int Test<T1,T2>(Func<T1,T2,int>func,T1 a,T2 b) { return func(a, b); }
(4) .predicate
bool return type of predicate generic delegate
predicate <int> expressed its argument is int return bool delegate
Predicate and only one parameter, the return value is fixed to bool
例:public delegate bool Predicate<T> (T obj)
2. Use of delegation
(1) .Delegate using
public delegate int MethodDelegate(int x, int y); private static MethodDelegate method; static void Main(string[] args) { method = new MethodDelegate(Add); Console.WriteLine(method(10,20)); Console.ReadKey(); } private static int Add(int x, int y) { return x + y; }
(2) .Action use
static void Main(string[] args) { Test<string>(Action,"Hello World!"); Test<int>(Action, 1000); Test<string>(p => { Console.WriteLine("{0}", p); }, "Hello World");//使用Lambda表达式定义委托 Console.ReadKey(); } public static void Test<T>(Action<T> action, T p) { action(p); } private static void Action(string s) { Console.WriteLine(s); } private static void Action(int s) { Console.WriteLine(s); }
You can use Action <T1, T2, T3, T4> delegate transmission method as a parameter instead of explicitly declared custom delegate. The method of packaging must be defined with this method signature delegate correspond. That is, the method of the package must have four parameters are passed to it a value, and can not return a value. (In C #, the method must return void) In general, such methods for performing an operation.
(3) .Func use
static void Main(string[] args) { Console.WriteLine(Test<int,int>(Fun,100,200)); Console.ReadKey(); } public static int Test<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b) { return func(a, b); } private static int Fun(int a, int b) { return a + b; }
(4). Predicate using
Generic delegate: that define a set of conditions and determine whether compliance with the specified object of these conditions. This delegate is used by several methods and Array List class, for searching within the collection.
static void Main(string[] args) { Point[] points = { new Point(100, 200), new Point(150, 250), new Point(250, 375), new Point(275, 395), new Point(295, 450) }; Point first = Array.Find(points, ProductGT10); Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y); Console.ReadKey(); } private static bool ProductGT10(Point p) { if (p.X * p.Y > 100000) { return true; } else { return false; } }
Predicate delegate search using an array of Point structures with Array.Find methods. If the product of the X and Y fields greater than 100,000, represented by this delegate ProductGT10 method returns true. Find method to each element of the array to call this delegate, stopped at a point in line with the first test condition.
3. commissioned empty
(1) In the empty class Shen Mingqing delegate method, followed by cycle referenced by the delegate removed.
Methods as below:
public MethodDelegate OnDelegate; public void ClearDelegate() { while (this.OnDelegate != null) { this.OnDelegate -= this.OnDelegate; } }
(2) If there is no way Shen Mingqing empty delegate in a class, we can use GetInvocationList check out the delegate references, and then removed.
Methods as below:
public MethodDelegate OnDelegate; static void Main(string[] args) { Program test = new Program(); if (test.OnDelegate != null) { System.Delegate[] dels = test.OnDelegate.GetInvocationList(); for (int i = 0; i < dels.Length; i++) { test.OnDelegate -= dels[i] as MethodDelegate; } } }
4. Principal characteristics
Delegates are similar to C ++ function pointers, but they are type-safe.
The method allows to delegate passed as parameters.
Delegate can be used to define the callback methods.
Delegate can be linked together; for example, you can call multiple methods for an event.
The method does not have to exactly match the delegate signature.
5. Summary:
Delegate parameter at least 0, up to 32 parameters, no return value, the return value may specify the type
Func acceptable 0-16 pass parameters, return value must have
Action can accept 0-16 pass parameters, return value None
Predicate only accept an incoming parameters, return values of type bool