zoukankan      html  css  js  c++  java
  • 委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解

    1.使用简单委托

    namespace 简单委托
    {
        class Program
        {
            //委托方法签名
            delegate void MyBookDel(int a);
            //定义委托
            static MyBookDel myBookDel;
            //普通方法
            public static void MathBook(int a)
            {
                Console.WriteLine("我是数学书" + a);
            }
            static void Main(string[] args)
            {
                myBookDel += MathBook;
                myBookDel(3);
                Console.ReadKey();
            }
        }
    }

    2.Action委托

    /// <summary>
    /// Action委托的优势是在于不用先去定义 方法签名 然后再用,可以直接使用
    /// 他把他所支持的参数类型写在了<>里面,所以不需要单独去定义方法签名了
    /// 不过Action不支持返回值
    /// </summary>
    namespace Action委托
    {
        class Program
        {
            static Action<int> ac;
            public static void MathBook(int a)
            {
                Console.WriteLine("数学" + a);
            }
            static void Main(string[] args)
            {
                ac += MathBook;
                ac(6);
                Console.ReadKey();
            }
        }
    }

    3.Func委托

    /// <summary>
    /// Func委托几乎和Action一样,唯一的区别就是Func支持返回值
    /// Func的<>里面的前面的是参数,最后一个值永远代表返回值的类型
    /// </summary>
    namespace Func委托
    {
        class Program
        {
            static Func<int, string> func;
            public static string MyBook(int a)
            {
                return "哈哈哈" + a;
            }
            static void Main(string[] args)
            {
                func += MyBook;
                Console.WriteLine(func(1));
                Console.ReadKey();
            }
        }
    }

    4.匿名方法

    /// <summary>
    /// 匿名方法就是写委托时,不想再单独定义一个方法体,然后再进行+= 的一步操作,直接把方法体写在+= 的后面,这种方法就叫匿名方法
    /// </summary>
    namespace 匿名方法
    {
        class Program
        {
            delegate int NiMingDel(int a, int b);
            static NiMingDel nimingDel;
            static void Main(string[] args)
            {
                nimingDel += delegate (int x, int y)  { return x + y; };
                //计算1 + 2
                Console.WriteLine(nimingDel(1, 2));
                Console.ReadKey();
            }
        }
    }

    5.lambda表达式

    /// <summary>
    /// 匿名方法用lambda表达式去代替
    /// = (参数) => {方法体}
    /// </summary>
    namespace 合体
    {
        class Program
        {
            delegate int NiMingDel(int a, int b);
            static NiMingDel nimingDel;
            static void Main(string[] args)
            {
                //一种语法糖
                nimingDel = (x, y) => x + y; 
                //计算1 + 2
                Console.WriteLine(nimingDel(1, 2));
                Console.ReadKey();
            }
        }
    }

    6.合体

    /// <summary>
    /// Func<> 与 lambda表达式的合体
    /// </summary>
    namespace _6.合体
    {
        class Program
        {
            static void Main(string[] args)
            {
                Func<int, int, string> func = (x, y) => { return "计算结果是:" + (x + y); };
                Console.WriteLine(func(1, 2));
                Console.WriteLine("c#牛逼!");
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    8.22
    webstrom安装流程
    8.21
    8.20
    8.20学习笔记
    使用WebClient异步获取http资源
    导航栏,可直接使用
    asp.net mvc5实现单点登录
    使用C#调用Word的接口生成doc文件与html文件
    下载网页并保存
  • 原文地址:https://www.cnblogs.com/sanyejun/p/7867913.html
Copyright © 2011-2022 走看看