• 匿名方法


    一、概念:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace EventDemo
    {
        class Program
        {
            // 定义投票委托
            delegate void VoteDelegate(string name);
    
            static void Main(string[] args)
            {
                // 实例化委托对象
                //VoteDelegate votedelegate = new VoteDelegate(new Friend().Vote);
    
                // 使用匿名方法的代码
                // 匿名方法内联了一个委托实例(可以对照上面的委托实例化的代码来理解)
                // 使用匿名方法后,我们就不需要定义一个Friend类以及单独定义一个投票方法
                // 这样就可以减少代码量,代码少了,阅读起来就容易多了,以至于不会让过多的回调方法的定义而弄糊涂了
                VoteDelegate votedelegate = delegate(string nickname)
                {
                    Console.WriteLine("昵称为:{0} 来帮Learning Hard投票了", nickname);
                };
    
                // 通过调用托来回调Vote()方法
                votedelegate("SomeBody");
                Console.Read();
            }
        }
    }
    View Code

    二、使用匿名方法来忽略委托参数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace EventDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Timer类在应用程序中生成定期事件
                System.Timers.Timer timer = new System.Timers.Timer();
                // 该值指示是否引发Elapsed事件
                timer.Enabled = true;
                // 设置引发Elapsed事件的间隔
                timer.Interval = 1000;
                // Elapsed事件是达到间隔时发生,前面设置了时间间隔为1秒,
                // 所以每一秒就会触发Elapsed事件,从而回调timer_Elapsed方法,输出当前的时间
                // timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
                // 此时timer_Elapsed方法中的参数根本就不需要,所以我们可以使用匿名方法来省略委托参数
                // 省略了参数后我们的代码就更加简洁了,看的多舒服啊
                // 在开发WinForm程序中我们经常会用不到委托的参数,此时就可以使用匿名方法来省略参数
                timer.Elapsed += delegate
                {
                    Console.WriteLine(DateTime.Now);
                };
                Console.Read();
            }
            public static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Console.WriteLine(DateTime.Now);
            }
        }
    }
    View Code

    三、在匿名方法中捕捉变量

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace EventDemo
    {
        class Program
        {
            // 定义闭包委托
            delegate void ClosureDelegate();
    
            static void Main(string[] args)
            {
                ClosureDelegate test = CreateDelegateInstance();
                test();
    
                Console.Read();
            }
    
            // 闭包延长变量的生命周期
            private static ClosureDelegate CreateDelegateInstance()
            {
                int count = 1;
    
                ClosureDelegate closuredelegate = delegate
                {
                    Console.WriteLine(count);
                    count++;
                };
    
                // 调用委托            
                closuredelegate();
                return closuredelegate;
            }
        }
    }
    View Code
  • 相关阅读:
    centos下部署启动elasticsearch错误集合与解决方案
    rpm和yum的区别
    centos7 安装Jdk1.8.0
    nvm-windows安装
    linux运行.sh命令
    centos7 安装 nvm
    centos7 安装、使用git
    转载:centos安装redis
    centos7 安装Node.js并配置为全局可用
    前端代码tomcat下简单部署
  • 原文地址:https://www.cnblogs.com/scmail81/p/8683283.html
Copyright © 2020-2023  润新知