zoukankan      html  css  js  c++  java
  • 匿名委托

    Action

    系统封装的Action委托,没有参数没有返回值。调用实例为:

    class Program
      {
        public delegate void Action();
        static void Main(string[] args)
        {
          Action action = new Action(Method);
          action();
        }
        private static void Method()
        {
          Console.WriteLine("i am is a action");
        }
      }
    

    如果方法的表达很简单,可以使用Lambda表达式,代码如下:

    Action action = () => { Console.WriteLine("i am is a action"); };
                action();

    Action<T>

    系统封装的Action<T>委托,有参数但是没有返回值。调用实例为:

    class Program
      {
        public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
        static void Main(string[] args)
        {
          Action<int,int> action = new Action<int,int>(Method);
          action(2,3);
        }
        private static void Method(int x,int y)
        {
          Console.WriteLine("i am is a action");
        }
      }
    

    使用Lambda表达式编写Action<T>代码如下:

    Action<int, int> action = (x, y) => { Console.WriteLine("i am is a action"); };
                action(2,3);

    Fun<T>

    系统封装的Fun<T>委托,有参数而且返回值。调用实例为:

    class Program
      {
        public delegate TResult Fun<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
        static void Main(string[] args)
        {
          Fun<int, int, bool> fun = new Fun<int, int, bool>(Method);
          bool ret = fun(2,3);
        }
        private static bool Method(int x,int y)
        {
          if (x + y == 5)
          {
            return true;
          }
          else
          {
            return false;
          }
        }
      }
    

    使用Lambda表达式编写Fun<T>,代码如下:

    Fun<int, int, bool> fun = (x, y) =>
          {
            if (x + y == 5)
            {
              return true;
            }
            else
            {
              return false;
            }
          };
          bool ret = fun(2,3);
  • 相关阅读:
    HDU 5313 bitset优化背包
    bzoj 2595 斯坦纳树
    COJ 1287 求匹配串在模式串中出现的次数
    HDU 5381 The sum of gcd
    POJ 1739
    HDU 3377 插头dp
    HDU 1693 二进制表示的简单插头dp
    HDU 5353
    URAL 1519 基础插头DP
    UVA 10294 等价类计数
  • 原文地址:https://www.cnblogs.com/zengpeng/p/4276562.html
Copyright © 2011-2022 走看看