委托与之扩展

一:委托

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

namespace 委托
{
    class Program
    {
        //创建委托
        delegate int NumChan(int i);
        static void Main(string[] args)
        {
            //创建委托实例
            NumChan c = new NumChan(Num);
            c(50);
            Console.WriteLine("值为:{0}",GetNum());
            Console.ReadKey();
        }
        //1.相当于C或C++中函数的指针;
        //2.对某个方法的引用的一种引用类型变量,可以在运行时被改变;
        //3.用于实现事件和回调方法;
        //4.派生自System.Delegate类;
        //5.声明委托:delegate 类型  变量名(参数)

        static int num = 15;
        //创建一个方法
        public static int Num(int o)
        {
            num += o;
            return num;
        } 
        //输出
        public static int GetNum()
        {
            return num;
        }
    }
}
结果:

C:\Users\Administrator\Desktop

二:多播委托

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

namespace 多播委托
{
    class Program
    {
        //定义委托
        delegate int Get(int i);
        static void Main(string[] args)
        {
            //实例化委托
            Get gg;
            Get g = new Get(Num1);
            Get g1 = new Get(Num2);
            gg = g;
            gg += g1;
            g(50);
            //调用输出
            Console.WriteLine("职位:{0}",Result());
            Console.ReadKey();

        }
        //1.创建一个委托被调用时要调用的方法的调用列表;
        static int N = 10;
        //创建方法
        public static int Num1(int T)
        {
            N *= T;
            return N;
        }
        public static int Num2(int t)
        {
            N /= t;
            return N;
        }
        //输出
        public static int Result()
        {
            return N;
        }
    }
}
结果:

C:\Users\Administrator\Desktop

三:委托链

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

namespace 委托链
{
    class Program
    {
        /// <summary>
        /// 定义一个委托
        /// </summary>
        /// <param name="args"></param>
        public delegate void Dels();

        static void Main(string[] args)
        {
            //调用实例委托
            Dels d = new Dels(Program.Num1);
            Dels d1 = new Dels(new Program().Num2);
            //定义委托对象
            Dels dd = null;
            //链接委托
            dd += d;
            dd += d1;
            //移除委托
           // dd -= d;
            //调用委托链
            dd();
            Console.Read();
        }
        //委托链(多路广播委托)也是一种委托类型,本质是把多个委托连接在一起;
        private static void Num1()
        {
            Console.WriteLine("方法一");
        }
        private  void Num2()
        {
            Console.WriteLine("方法二");
        }
    }
}
结果:

C:\Users\Administrator\Desktop

移除委托结果:

C:\Users\Administrator\Desktop

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值