110,538
社区成员
发帖
与我相关
我的任务
分享
public delegate void BoilerLogHandler(string status);
// 基于上面的委托定义事件
public event BoilerLogHandler BoilerEventLog;
Action<string> methodCall = (x) => { x += "haha"; Console.WriteLine(x);};
public event Action<string> BoilerEventLog;
static void Main(string[] args)
{
BoilerEventLog += new Action<string>(methodCall);
BoilerEventLog("123");
Console.ReadKey();
}
static Action<string> methodCall = (x) => { x += "haha"; Console.WriteLine(x); };
public static event Action<string> BoilerEventLog;
static void Main(string[] args)
{
BoilerEventLog += new Action<string>(methodCall);
BoilerEventLog("123");
Console.ReadKey();
}
static Action<string> methodCall = (x) => { x += "haha"; Console.WriteLine(x); };
public static event Action<string> BoilerEventLog;
Action<string> methodCall = (x) => { x += "haha"; MessageBox.Show(x); };
public event Action<string> BoilerEventLog;
private void button2_Click(object sender, EventArgs e)
{
BoilerEventLog += new Action<string>(methodCall);
BoilerEventLog("123");
}
public event Func<string, string, string> MyFuncEvent;
public event Action<object> MyActionEvent;
protected virtual void OnMyFuncEvent(string arg1,string arg2)
{
var ret = MyFuncEvent?.Invoke(arg1, arg2);
}
protected virtual void OnMyActionEvent(object arg)
{
MyActionEvent?.Invoke(arg);
}
//声明
public delegate int getTotal(int x,int y);
//方法里用
getTotal get=new getTotal((a,b)=>a+b);
int total=get(100,200);
//使用Func,一句完成,省去声明
int total=new Func<int,int,int>((a,b)=>a+b);
再给你个例子
public event Func<int, string> clickMe;
private void button2_Click(object sender, EventArgs e)
{
clickMe += new Func<int, string>(x => Convert.ToString(x, 2));
string str=clickMe(17); //output:10001
}