C#中委托类型

首先声明一个委托,使用时需要使用new操作符来创建一个委托实例,在使用实例中,可以封装一个静态方法,也可以封装一个非静态方法,然后把委托指向要引用的方法,最好就可以在程序中像调用方法一样应用委托的实例对象调用它指向的方法。

举例1:

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

delegate void mydelegate();   //指向一个void类型的函数原型的委托
public class delegatetest       //具有实例化方法和静态方法的类
{
    public void instancemethod()   //实例化方法
    {
        Console.WriteLine("a message from the instance method.");
    }
    static public void staticmethod()    //静态方法
    {
        Console.WriteLine("a message from the static method.");
    }
}

public class maintest
{
    public static void Main()
    {
        delegatetest p = new delegatetest();  //生成具有实例化方法和静态方法的对象
        mydelegate myd = new mydelegate (delegatetest.staticmethod);   //把委托映射到静态方法
        myd();                          //静态方法的调用
        myd = new mydelegate (p.instancemethod);   //把委托映射到实例方法中
        myd();                          //实例方法的调用
        Console.ReadLine();
    }
}

举例2:

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

namespace Delegate
{
    public delegate void GreetingDelegate(string name);       //定义委托,它定义了可以代表的方法的类型
    public class GreetingManager                     //新建的GreetingManager 类
    {
        public void GreetPeople(string name, GreetingDelegate MakeGreeting)
        {
            MakeGreeting(name);
        }
    }
    class Program
    {
        public static void EnglishGreeting(string name)
        {
            Console.WriteLine("Morning, " + name);
        }
        public static void ChineseGreeting(string name)
        {
            Console.WriteLine("早上好, " + name);
        }
    }
    class test
    {
        static void Main(string[] args)
        {
            GreetingManager gm = new GreetingManager();
            gm.GreetPeople("Jimmy Zhang",Program . EnglishGreeting);   
            gm.GreetPeople("张子阳",Program . ChineseGreeting);
            Console.ReadLine();
        }
    }
}

多重委托:把委托同时指向多个方法,这些方法必须有相同的参数,返回方法必须是void类型,"+"运算符来调用ccombine(),“-”来调用remove()

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

delegate void dg(string s);

class A
{
    public static void H(string str)
    {
        Console.WriteLine("\t\t hello,{0}", str);
    }
    public static void G(string str)
    {
        Console.WriteLine("\t \t goodbye,{0}", str);
    }
}

class myclass
{
    public static void Main()
    {
        dg a, b, c, d;
        a = new dg(A.H);
        b = new dg(A.G);
        c = a + b;
        d = c - a;
        Console.WriteLine("invoke delegate a:");
        a("A");
        Console.WriteLine("invoke delegate b");
        b("B");
        Console.WriteLine("invoke delegate C");
        c("C");
        Console.WriteLine("invoke delegate d");
        d("D");
        Console.ReadLine();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值