美文网首页
C# delegate Func Action 之间的关系

C# delegate Func Action 之间的关系

作者: 贼噶人 | 来源:发表于2019-04-12 12:37 被阅读0次

其实可以把Func、Action都可以理解成是delegate,只是为了方便使用的“语法糖”

delegate的使用

  delegate int Add(int m,int n);
  
  int FAdd(int m,int n){
    return m + n;
  }  

var add  = new Add(FAdd);
add (1,2);

Action的使用 是一种不带返回参数的delegate,而且避免用户进行delegate的定义,直接可以使用

    video sayHello(string name){
      Console.WriteLine($"{name} Hello!");
  }  

Action<string> action = new Action<string>(sayHello);
action("bruce");

Func和Action的区别在于有返回值

    int FAdd(int m,int n){
      Console.WriteLine($"{m + n}");
  return m + n;
  }  

Func<int,int,int> func= new Action<int,int,int>(FAdd);
Console.WriteLine(func(1,2));

相关文章

网友评论

      本文标题:C# delegate Func Action 之间的关系

      本文链接:https://www.haomeiwen.com/subject/imxcwqtx.html