c#事件(Event)里的委托(Delegate)能用Func<> 或者Action<>代替么?

qq_36483141 2017-07-05 11:15:41
在类的内部声明事件,首先必须声明该事件的委托类型。
public delegate void BoilerLogHandler(string status);

// 基于上面的委托定义事件
public event BoilerLogHandler BoilerEventLog;


那么,委托(Delegate)能用Func<> 或者Action<>代替么?
...全文
1004 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36483141 2017-07-06
  • 打赏
  • 举报
回复
     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 后,竟然可以编译, 如代码二.
委托,事件有这样的写法?
 static  Action<string> methodCall = (x) => { x += "haha"; Console.WriteLine(x); };
public static event Action<string> BoilerEventLog;






正怒月神 2017-07-06
  • 打赏
  • 举报
回复
引用 4 楼 qq_36483141 的回复:
谢谢版主! 我想把你的代码改成控制台的,都没有成功,版主指点一下。
这要看你代码是怎么写的了
threenewbee 2017-07-06
  • 打赏
  • 举报
回复
C# 4里,事件本身有泛型的版本,可以直接用 EventHandler<T>
正怒月神 2017-07-05
  • 打赏
  • 举报
回复
可以啊 本来fun和action就是委托用的。fun针对有返回类型的,action代表void 写了个action,至于func是大同小异的,就不写了
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");
        }
闭包客 2017-07-05
  • 打赏
  • 举报
回复
转到 Action 和 Func 的定义可以看到,它们本来就是 delegate
bloodish 2017-07-05
  • 打赏
  • 举报
回复
当然可以,任何委托(delegate)都可以用来声明事件

 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);
        }
秋的红果实 2017-07-05
  • 打赏
  • 举报
回复
更正下:int total = new Func<int, int, int>((a, b) => a + b)(100,200);
秋的红果实 2017-07-05
  • 打赏
  • 举报
回复
可以 不过他们之间有区别,用func,action,predicate方便(比delegate),但有限制,前两个最多可以接受16个输入参数,func有一个返回参数,action没有,predicate一个输入、一个返回参数(必须是bool型的) 说方便,请看代码

//声明
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
}
qq_36483141 2017-07-05
  • 打赏
  • 举报
回复
引用 3 楼 hanjun0612 的回复:
可以啊 本来fun和action就是委托用的。fun针对有返回类型的,action代表void 写了个action,至于func是大同小异的,就不写了
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");
        }
谢谢版主! 我想把你的代码改成控制台的,都没有成功,版主指点一下。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧