0
点赞
收藏
分享

微信扫一扫

C#之委托:delegate

一条咸鱼的干货 2022-03-30 阅读 76
c#

delegate

  • 委托:将方法作为变量或参数进行传递,并以方法的形式进行执行;
//带参数并且无返回值的委托
public delegate void SayHelloFunc(string name);
namespace CPlusLesson
{
    class Test01
    {
        static void Main(string[] args)
        {
            //委托的声明:(注意:有参无返回值的委托对应有参无返回值的方法;——要相对应)
            SayHelloFunc hel = SayHelloName;
            hel("张三");

            Console.ReadKey();
        }
        public static void SayHelloName(string name)
        {
            Console.WriteLine("你好啊!" + name);
        }
    }
}

在这里插入图片描述

委托链

  • hel+=SayHelloPople;
class Test01
    {
        static void Main(string[] args)
        {
            //委托的声明:(注意:有参无返回值的委托对应有参无返回值的方法;——要相对应)
            SayHelloFunc hel = SayHelloName;

            hel += SayHelloPople;
            hel("张三");

            Console.ReadKey();
        }
        public static void SayHelloName(string name)
        {
            Console.WriteLine("你好啊!" + name);
        }
        public static void SayHelloPople(string p)
        {
            Console.WriteLine("你好啊!" + p+"老师");
        }
    }

在这里插入图片描述

举报

相关推荐

0 条评论