Unity Base C # Basics--[to] plain English Series C # Delegate and event tutorial (i.)

Source: Internet
Author: User
Tags ming ticket microsoft c

From the preface, we should know a little bit about the importance of the Commission and the event, although we can still blur it, but from my plain English series, I will make these concepts easy to understand. First of all, we should first talk about the Commission, literally understand, as long as the Chinese should know this meaning, unless the commission of the 2 characters do not know, for example, Xiao Ming entrusted to buy a ticket.

But is it so simple in our program world? Of course, what is Oo,oo is the real world of abstraction, all the real world things, in OO can get a very good interpretation, sorry, pull away ...

Then we straightforward, if the students do not wearies, you can look at the online Chinese, the English greetingpeople example.

The first is the C # syntax

public delegate void Bugticketeventhandler ();

Delegate is the key word, "Note: EventHandler is a statement commissioned by the Microsoft C # naming standard, my habit is the standard will be the first time to say that the naming habit to be the first time to develop"

It's over, this is the Commission, it's so simple, see what the difference is with our general approach?

public void Buyticket ()
{
Method body
}

The difference is that in real life, a delegate is just a command, to do something else, and in the program world, the delegate just stores the address of each method, and he does nothing.

Then we put that, Xiao Ming commissioned a small Zhang to buy tickets to the real-life scene, how in the program world embodiment?

code highlighting produced by Actipro Codehighlighter (freeware) http://www.codehighlighter.com/-->//Xiao Zhang Class     Public classMrzhang {//In fact, the sad figure of buying a ticket is Xiao Zhang         Public Static voidBuyticket () {Console.WriteLine ("NND, let me go to buy tickets every time, chicken man! "); }    }    //Xiao Ming class    classmrming {//declaring a delegate is, in fact, a "command."         Public Delegate voidBugticketeventhandler ();  Public Static voidMain (string[] args) {            //here is the specific explanation of what this command is doing, this example is Mrzhang.buyticket "small ticket buy Ticket"Bugticketeventhandler MyDelegate =NewBugticketeventhandler (Mrzhang.buyticket); //at this time the delegate was attached to a specific methodMyDelegate ();        Console.readkey (); }    }

Bugticketeventhandler mydelegate = new Bugticketeventhandler (mrzhang.buyticket);

This is the declaration method of the delegate, Bugticketeventhandler (the delegate method), and the delegate's method must be added because the delegate's constructor is not empty.

Note: The parameter and return type of the delegate must be the same as the method you want to delegate, for example:

public delegate void Bugticketeventhandler ();


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

Students, see here can digest digestion first, rest, we simply talk about the concept of the chain of trust:

In fact, the delegation chain is quite simple, in real life, Xiaoming called Xiao Zhang to buy the ticket, may then let him take a movie ticket,

And the expression in our program World is:

Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-//Xiao Zhang Class     Public classMrzhang {//In fact, the sad figure of buying a ticket is Xiao Zhang         Public Static voidBuyticket () {Console.WriteLine ("NND, let me go to buy tickets every time, chicken man! "); }         Public Static voidBuymovieticket () {Console.WriteLine ("I'll go, pick up the girls, and let me bring the movie tickets! "); }    }    //Xiao Ming class    classmrming {//declaring a delegate is, in fact, a "command."         Public Delegate voidBugticketeventhandler ();  Public Static voidMain (string[] args) {            //here is the specific explanation of what this command is doing, this example is Mrzhang.buyticket "small ticket buy Ticket"Bugticketeventhandler MyDelegate =NewBugticketeventhandler (Mrzhang.buyticket); MyDelegate+=Mrzhang.buymovieticket; //at this time the delegate was attached to a specific methodMyDelegate ();        Console.readkey (); }    }

In fact, we just added mydelegate + = Mrzhang.buymovieticket in the program;

At this time this Commission is equivalent to do 2 things, first buy tickets, and then buy movie tickets pull!
Well, about plain English commissioned on the introduction to here, is not not so scary as everyone thought, the next time we will go on to plain English of the event of the explanation.

Unity Base C # Basics--[to] plain English Series C # Delegate and event tutorial (i.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.