A12_获取与清空委托列表|匿名委托|Lambda表达式

本文介绍C#中委托的使用方法,包括如何获取委托列表并逐一调用,以及如何清空委托列表。此外,还探讨了匿名委托和Lambda表达式的应用。

/*
 * 委托的取值与清空委托列表
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A12_GetAndCleanDelegateList
{
    class Demo9
    {
        private Action actHandler;

        public Demo9()
        {
            actHandler += DelegateMethod1;
            actHandler += DelegateMethod2;
            actHandler += DelegateMethod3;
        }

        public void DelegateMethod1()
        {
            Console.WriteLine("Method DelegateMethod1");
        }

        public void DelegateMethod2()
        {
            Console.WriteLine("Method DelegateMethod2");
        }

        public void DelegateMethod3()
        {
            Console.WriteLine("Method DelegateMethod3");
        }

        //取得委托列表
        public void Test1()
        {
            Delegate[] delArray = actHandler.GetInvocationList();
            foreach (Delegate item in delArray)
            {
                item.DynamicInvoke();
            }
        }

        //清空委托列表
        public void Test2()
        {
            while (actHandler!=null)
            {
                actHandler -= this.actHandler;
            }

            if (actHandler != null)
            {
                actHandler.Invoke();
            }
            else
            {
                Console.WriteLine("委托列表已经清空");
            }
        }


        //清空委托列表通用算法
        public void Test3()
        {
           Delegate[] delArray = actHandler.GetInvocationList();
           for (int i = 0; i < delArray.Length; i++)
           {
                actHandler -= delArray[i] as Action;
           }

            if (actHandler != null)
            {
                actHandler.Invoke();
            }
            else
            {
                Console.WriteLine("Demo9:Test3() 委托列表已经清空");
            }
        }


        static void Main1(string[] args)
        {
            Demo9 obj = new Demo9();
            obj.Test1();
            //obj.Test2();
            obj.Test3();
        }
    }
}


/*
 * 匿名委托
 * 如果一个委托不需要在别处调用,那么使用匿名方法可以减少代码量
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A12_GetAndCleanDelegateList
{
    class Demo10
    {

        public void Test2()
        {
            Func<int, int, int> funAddingMethodHandler = delegate (int num1, int num2)
            {
                return num1 + num2;
            };

            int result = funAddingMethodHandler(20, 30);
            Console.WriteLine(result);
        }

        static void Main1(string[] args)
        {
            Demo10 obj = new Demo10();
            obj.Test2();
        }
    }
}


/*
 * Lambda表达式
 * 对于匿名委托的进一步简化
 * 1.去掉delegate关键字
 * 2.去掉参数列表中的类型定义
 * 3.如果方法中只有一条语句,可以去掉return和{};
 * 4.
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A12_GetAndCleanDelegateList
{
    class Demo11
    {
        public void Test1()
        {
            //匿名委托
            Func<int, int, int> funAddingMethodHandler = delegate (int num1, int num2)
            {
                return num1 + num2;
            };

            //Lambda表达式

            Func<int, int, int> funAddingMethodHandler2 = (int num1, int num2) =>
            {
                return num1 + num2;
            };


            Func<int, int, int> funAddingMethodHandler3 = (num1, num2) =>
            {
                return num1 + num2;
            };

            Func<int, int, int> funAddingMethodHandler4 = (num1, num2) => num1 + num2;

            //方法中只有一个参数,可以去掉()
            Func<int, int> funAddingMethodHandler5 = (num1) => num1 + 888;

            Func<int, int> funAddingMethodHandler6 = num1 => num1 + 888;

            //如果没有参数
            Func<int> funAddingMethodHandler7 = () => 888;

            Console.WriteLine(funAddingMethodHandler(10, 20));
            Console.WriteLine(funAddingMethodHandler2(80, 20));
            Console.WriteLine(funAddingMethodHandler3(80, 30));
            Console.WriteLine(funAddingMethodHandler4(80, 40));
            Console.WriteLine(funAddingMethodHandler5(200));
            Console.WriteLine(funAddingMethodHandler6(20));
            Console.WriteLine(funAddingMethodHandler7());
        }

        //Lambda表达式使用外部变量
        public void Test2()
        {
            int num = 100;
            Func<int, int> funHandler = x => x + num; // 其中num表达式外部变量
            Console.WriteLine(funHandler(1000));
        }

        static void Main1(string[] args)
        {
            Demo11 obj = new Demo11();
            obj.Test1();
            obj.Test2();

        }
    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值