c# 匿名委托

发布时间 2023-05-23 17:49:50作者: 程序员小白n

无参:

 if (this.tBReciveInfo.InvokeRequired)
                {
                    this.Invoke(new Action(()=>{
                        this.tBReciveInfo.Text = message.Body.ToString();
                    }));
                }

 

一个参数:

1  private void act()
2  {
3      this.invoke(new Action<string>(s=>{方法(s);}),"参数s")
4  }

万能委托在原有基础上,更进一步,如下:

1 private void act()
2 {
3     this.invoke(new Action<string>(s=>{方法1(参数1);方法2(参数2);……方法n;}),"")
4 }

这里设置s的值为空值,把需要传的参数,直接写在方法里,做到多个参数传值的效果,而不用考虑s的值。