First, give a delegate scene: hot weather, two Gouzi want to buy a bottle of iced cola, but two Gouzi lazy, so he went looking for someone on his behalf, then there must be an agent.
Before creating the agent to define a delegate: public delegate string BuyColaDelegate (string drink);
Creating Agent: BuyColaDelegate delegate = new BuyColaDelegate (); (error code)
So the question is, an agent needs to bind a method ah, otherwise he knows he went doing, so you need to create a real agent:
BuyColaDelegate delegate = new BuyColaDelegate(BuyCola);
public static string BuyCola(string drink)
{
try
{
WriteLine($"购买:{drink}");
return "购买完成";
}
catch (Exception ex)
{
WriteLog(LogType.ERROR, "Program", "BuyCola", ex.ToString());
return null;
}
}
Above is to create a real agent, then the agent standing by in reserve. Two Gouzi to the agent then issued an order to buy a Coke:
string res = delegate ( "coke");
When res == "complete purchase", two Gouzi it comfortable to drink iced cola, NM, just Shuangwai.
Note:
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.
We used a declarative delegate to, trust parameters at least 0, up to 32 parameters, no return value, the return value can also be specified type.
Second, the two Gouzi a little depressed, I'll just buy a bottle of Coke, I would also like to engage an agent, sick of it. There are no ready agents, ah, I used to use, Action appearances:
Action ac1 = new Action (); (error code)
The agents seem to be somewhat weak chicken, with not a parameter, but also can not have a return value.
static void BuyCola public ()
{
the try
{
the WriteLine ( "later: Coke");
}
catch (Exception ex)
{
WriteLog(LogType.ERROR, "Program", "BuyCola", ex.ToString());
}
}
Action ac1 = new Action(BuyCola);
Then it Shuangwai two Gouzi to an agent issued an order to buy Coke: ac1 ();
Note:
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.
Three, two Gouzi lazy and depressed child (how old are depressed), drinking cola, iced watermelon how I'd also like to do, do I then put forward a BuyWatermelon (),
Then write parameters inside dead? Do not! Another Action play:
Action <string> ac2 = new Action <string> (); (error code)
public static void BuyDrink(string drink)
{
try
{
WriteLine($"购买:{drink}");
}
catch (Exception ex)
{
WriteLog(LogType.ERROR, "Program", "BuyDrink", ex.ToString());
}
}
Action<string> ac2 = new Action<string>(BuyDrink);
Then again Shuangwai two Gouzi to an agent issued an order to buy a watermelon: ac2 ( "watermelon"); two Gouzi successful, eat a watermelon (eat melon is not easy).
AC2 ( "Sprite"); AC2 ( "delicacies"); AC2 ( "Feast"); ........
Fourth, the two Gouzi is a thinking man, he was thinking agents to buy something, I have to know the results, ah, I let him buy watermelon, told me that did not buy back, who knows he is not half way to get rid of it ?
Func<string,string> func1 = new Func<string, string>(BuyDrink);
public static string BuyDrink(string drink)
{
try
{
WriteLine($"购买:{drink}");
return "purchase complete";
}
catch (Exception ex)
{
WriteLog(LogType.ERROR, "Program", "BuyDrink", ex.ToString());
return null;
}
}
Such two Gouzi know the results, the agent and my heart can only shout MMP.
Note:
Func generic delegates have returned values
Func <int> represents no arguments, returns an int value entrusted
Func <object, string, long> means that the incoming parameter object, string return value long delegate
Func <object, string, int> parameter means that the incoming object, string returns an int value entrusted
Func <T1, T2, T3, int> means that the incoming parameters T1, T2, T3 (generic) Returns delegate int
Func parameter of at least 0, up to 16 parameters, generic return by return. There must be a return value, not void, the last type parameter represents the return value type .
Fives,
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)
Predicate<string> pre1 = new Predicate<string>(XXX);