C#【委托/事件篇】委托经典实例

实例1:【基础使用】

using System;

namespace ConsoleApp1
{
    class Program
    {
        delegate void MyDel(int value);
        void PrintLow(int value)
        {
            Console.WriteLine("{0}-Low Value", value);
        }
        void PrintHigh(int value)
        {
            Console.WriteLine("{0}-High Value", value);
        }
        static void Main(string[] args)
        {
            Program program = new Program();

            MyDel myDel;//申明委托变量

            Random rand = new Random();
            int randomValue = rand.Next(99);

            //创建一个包含PrintLow或PrintHigh的委托对象并将其赋值给myDel变量。
            //方法一:
            //myDel = randomValue < 50 ? new MyDel(program.PrintLow) : new MyDel(program.PrintHigh);

            //方法二:
            if (randomValue < 50)
            {
                myDel = program.PrintLow;
            }
            else
            {
                myDel = program.PrintHigh;
            }

            myDel(randomValue);//执行委托
            Console.ReadLine();

        }
    }
}


运行结果:

在这里插入图片描述

实例2:【为委托添加方法】

using System;

//为委托添加方法
namespace ConsoleApp2
{ 
    class Program
    {
        delegate void PrintFunction();
        class Test
        {
            public void Print1()
            {
                Console.WriteLine("Print1--instance");
            }
            public static void Print2()
            {
                Console.WriteLine("Print2--static");
            }
        }
        static void Main(string[] args)
        {
            Test t = new Test();//创建一个测试类实例
            PrintFunction pf;//创建一个空委托

            pf = t.Print1;//实例化并初始化该委托

            //给委托增加3个另外的方法
            pf += Test.Print2;
            pf += t.Print1;
            pf += Test.Print2;
            //现在,委托含有4个方法
            //pf -= Test.Print2;//移除委托

            if (null != pf)//确认委托有方法
                pf();//调用委托
            else
                Console.WriteLine("Delegate is Empty");

            Console.ReadLine();
        }
    }
}


运行结果:

在这里插入图片描述

实例3:【调用带返回值的委托】

using System;

//调用带返回值的委托
namespace ConsoleApp3
{
    delegate int MyDel();//声明有返回值的方法
    class MyClass
    {
        int IntValue = 5;
        public int Add2()
        {
            IntValue += 2;
            return IntValue;
        }
        public int Add3()
        {
            IntValue += 3;
            return IntValue;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            MyDel mDel = mc.Add2;//创建并初始化委托
            mDel += mc.Add3;//增加方法
            mDel += mc.Add2;//增加方法

            Console.WriteLine("Value:{0}", mDel());//调用委托并使用返回值
            Console.ReadLine();
            Console.ReadLine();
        }

    }
}

运行结果:

在这里插入图片描述

实例4:【调用带引用参数的委托】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//调用带引用参数的委托
namespace ConsoleApp4
{
    delegate void MyDel(ref int y);
    class MyClass
    {
        public void Add2(ref int y) { y += 2; }
        public void Add3(ref int y) { y += 3; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            MyDel mDel = mc.Add2;
            mDel += mc.Add3;
            mDel += mc.Add2;

            int x = 8;
            mDel(ref x);

            Console.WriteLine("Value:{0}", x);
            Console.ReadLine();
            Console.ReadLine();

        }
    }
}

运行结果:

在这里插入图片描述

实例5:【匿名方法(anonymous method)】

using System;

namespace ConsoleApp5
{
    //C#2.0引入匿名方法(anonymous method):是在初始化委托时内联声明的方法
    delegate int OtherDel(int param);
    class Program
    {
        static void Main(string[] args)
        {

            OtherDel del = delegate (int x)
            {
                return x + 20;
            };
            Console.WriteLine("{0}", del(5));
            Console.WriteLine("{0}", del(8));
            Console.ReadLine();
            Console.ReadLine();
        }
    }
}

运行结果:

在这里插入图片描述

实例6:【Lambada表达式(无名函数)】

using System;

//Lambada表达式,C#3.0引入,Lambda运算符“=>”读作“goes to”。使用希腊字母Lambda(λ)来表示无名函数。
namespace ConsoleApp6
{
    delegate double MyDel(int par);
    class Program
    {
        static void Main(string[] args)
        {

            MyDel del = delegate (int x) { return x + 1; };//匿名方法

            //Lambda表达式
            MyDel le1 = (int x) => { return x + 1; };
            MyDel le2 = (x) => { return x + 2; };
            MyDel le3 = x => { return x + 3; };
            MyDel le4 = x => x + 4;//最后选用,如果只有一个参数,可以省略圆括号,否则必须有括号;如果没有参数,必须使用一组空的圆括号。例如: MyDel le4 =() => {Action();};

            Console.WriteLine("{0}", del(12));
            Console.WriteLine("{0}", le1(12));
            Console.WriteLine("{0}", le2(12));
            Console.WriteLine("{0}", le3(12));
            Console.WriteLine("{0}", le4(12));

            Console.ReadLine();
            Console.ReadLine();
        }
    }
}

运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ElecNoon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值