Home >Backend Development >C#.Net Tutorial >C# delegate and event explanation

C# delegate and event explanation

高洛峰
高洛峰Original
2016-12-14 11:17:501333browse

From the preface, you should have some understanding of the importance of commissions and events. Although we can still be vague about it now, from my vernacular series, I will make these concepts easy to understand. First of all, let’s talk about delegation. Literally understood, all Chinese people should know this meaning, unless they don’t recognize the two Chinese characters for delegation. For example, Xiao Ming entrusts Xiao Zhang to buy a ticket.

But in our programming world, is it also so simple? Of course, what is OO? OO is the abstraction of the real world. All things in the real world can be well interpreted in OO. Sorry, I am going too far...

Then let’s get straight to the point. If students are not bored, you can take a look at the GreetingPeople example in Chinese and English on the Internet.

                                                                                                                                                                                                                                   out out over terresence to the C# syntax

public delegate void BugTicketEventHandler();

    out  . Cultivation】

It’s over, this is delegation, it’s that simple, let’s see what’s the difference from our normal method?

public void BuyTicket()
{   
 方法体
 }

You know the difference? In real life, a delegate is just a command, and someone else does the work. But in the programming world, the delegate just stores the address of each method, and he himself does nothing.

So let’s take the real-life scene where Xiao Ming entrusted Xiao Zhang to buy a ticket. How can it be reflected in the program world?

代码 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//小张类
    public class MrZhang
    {
        //其实买车票的悲情人物是小张
        public static void BuyTicket()
        {
            Console.WriteLine("NND,每次都让我去买票,鸡人呀!");
        }
    }

    //小明类
    class MrMing
    {
        //声明一个委托,其实就是个“命令”
        public delegate void BugTicketEventHandler();

        public static void Main(string[] args)
        {
            //这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”
            BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);

            //这时候委托被附上了具体的方法
            myDelegate();
            Console.ReadKey();
        }
    }

BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);

This is the declaration method of the delegate, BugTicketEventHandler (delegated method); the delegated method must be added because the delegated constructor is not empty.

Note: The parameters and return type of the delegate must be consistent with the method you want to delegate. For example:

public delegate void BugTicketEventHandler();


public static void BuyTicket()
{
Console.WriteLine( "NND, let me buy tickets every time, chicken man!");
}

Students, you can digest this and take a break. Let's briefly talk about the concept of delegation chain:

In fact, the delegation chain is also quite simple. In real life, after Xiao Ming asks Xiao Zhang to buy a ticket, he may then ask him to bring a movie ticket,

And the expression in our program world is:

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//小张类
    public class MrZhang
    {
        //其实买车票的悲情人物是小张
        public static void BuyTicket()
        {
            Console.WriteLine("NND,每次都让我去买票,鸡人呀!");
        }

        public static void BuyMovieTicket()
        {
            Console.WriteLine("我去,自己泡妞,还要让我带电影票!");
        }
    }

    //小明类
    class MrMing
    {
        //声明一个委托,其实就是个“命令”
        public delegate void BugTicketEventHandler();

        public static void Main(string[] args)
        {
            //这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”
            BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);

            myDelegate += MrZhang.BuyMovieTicket;
            //这时候委托被附上了具体的方法
            myDelegate();
            Console.ReadKey();
        }
    }

In fact, we Just added myDelegate += MrZhang.BuyMovieTicket;

in the program
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn