zoukankan      html  css  js  c++  java
  • C# 委托篇

    {
    //定义一个委托类型,如果传递的方法有参数此处必须声命,因为调用委托的时候将要用到
    delegate int AddDel(int a, int b);
    delegate int AddDel2(int a, int b);
    static void Main(string[] args)
    {
    #region 普通的委托
    ////调用静态方法
    //AddDel del = new AddDel(AddStaticFunc);  AddDel del=AddStaticFunc--这样写也可以
    //Console.WriteLine(del(1, 2));

    ////调用实例方法
    //AddDel2 del2 = new AddDel2(new Program().AddInstansFunc);
    //Console.WriteLine(del2(3, 4));

    //Console.ReadKey();
    #endregion

    #region 泛型委托
    //Func<int, int, int> func =AddStaticFunc; --注意委托指定方法的时候可以在new 委托(方法名)里指定 也可以直接如当前这种指定
    //凡是委托指定方法的地方都可以用lambda表达式,lambda语句,匿名方法
    //Func<int, int, int> func = new Func<int, int, int>((a, b) => a + b);----lambda 表达式
    //Func<int, int, int> func = new Func<int, int, int>(delegate(int a, int b) { return a + b; });---匿名方法
    Func<int, int, int> func = (int a, int b) => { return a + b; };// 注意在写lambda表达式的时候(){}可以省略的情况都是其里边只有一个变量或一个语句

    Console.WriteLine(func(1, 3));
    Console.ReadKey();
    #endregion

    }
    /// <summary>
    /// 静态方法
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    static int AddStaticFunc(int a, int b)
    {
    return a + b;
    }
    /// <summary>
    /// 实例方法
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    public int AddInstansFunc(int a, int b)
    {
    return a + b;
    }
    }

  • 相关阅读:
    睿象云-智能运维平台
    leetcode-----53. 最大子序和
    leetcode-----50. Pow(x, n)
    leetcode-----49. 字母异位词分组
    leetcode-----48. 旋转图像
    leetcode-----47. 全排列 II
    leetcode-----46. 全排列
    leetcode-----44. 通配符匹配
    SpringMVC @RequestParam和@RequestBody的区别
    SpringMVC 接受页面传递参数
  • 原文地址:https://www.cnblogs.com/luwei-s/p/4541703.html
Copyright © 2011-2022 走看看