【C#编程】委托

委托是一种可用于 封装命名匿命名方法引用类型

委托是一种 ,从代码上,我个人理解为给“规定的”方法( 静态方法,或是某个类的成员函数重命名 ,即被重命名的方法并不是随意的。

被委托的方法与委托的返回值类型与传递参数要一致

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

namespace delegation01
{
    class Program
    {
        public delegate void mydelegate(string mydelegate);  //委托声明

        class method
        {
            public void display(string name)                 //该方法为Method类的成员函数
            { 
                Console.WriteLine("委托人" + name);
            }
        }

        static void  display(string name)                    //该方法为静态方法
        {
            Console.WriteLine("委托人" + name);
        }

        static void Main(string[] args)
        {
            method dis = new method();
            mydelegate a = new mydelegate(dis.display);       //受委托的方法要么是某个类的成员,要么是静态方法

            mydelegate b = new mydelegate(display);

            a("A");
            b("B");

        }
    }
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值