3. Entrust the article

Source: Internet
Author: User
Tags microsoft c

1. Naming conventions
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"

2. Defining Delegates

Public (access modifier) delegate (delegate) int (return type) calleventhandler (int num1,int num2);

3. instantiation of a delegate

Calleventhandler call = new Calleventhandler (joins the method that needs to be associated);

4. Invoking Delegates

int result = Call (5,3)

5. Meaning: The method is called dynamically. A delegate is a reference to a method, which is the alias of a method, which means that the delegate behaves identically to the method.

6. Understanding:

In real life, a delegate is just a command, to do things is someone else, and in the program world, the delegate just stores the address of each method, and he himself is nothing to do.
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

Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-->//Xiao Zhang Class

public class Mrzhang {

In fact, the sad figure of buying a ticket is Xiao Zhang

public static void Buyticket ()

{

Console.WriteLine ("NND, every time let me go to buy tickets, chicken!" ");

}

}

Xiao Ming class

Class Mrming {

Declaring a delegate is, in fact, a "command."

public delegate void Bugticketeventhandler ();

public static void Main (string[] args) {

Here is the specific explanation of what this command is doing, this example is Mrzhang.buyticket "small ticket Buy Ticket"

Bugticketeventhandler mydelegate = new Bugticketeventhandler (mrzhang.buyticket);

At this time the delegate was attached to a specific method

MyDelegate ();

Console.readkey ();

}     }

7. Expansion

Since the delegate can be bound to a method, then there should be a way to remove the binding method, it is easy to think that this syntax is "-=";

8. Introduction

A delegate (delegate) is a reference type that we treat as a class to look at instead of a method, but a delegate is a reference to a method or a list of methods, and invoking a delegate instance is like invoking a pointer in C + +, and he encapsulates a reference to the development method. Or the delegate is the function of the bridge, the delegate object after the instance will pass the given parameter to the method that he callback, and execute the method.

3. Entrust the article

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.