Delegate, event Learning notes in C #

Source: Internet
Author: User

1. Entrusted delegate

The delegate delegate is also a type that can declare a delegate where any class can be declared, and it passes the method as a parameter of another method, so that different methods can be passed to accomplish different functions to make the program very extensible.


Suppose there is a computer, someone will use it to write programs, some people will use it to play games, some people will use it to watch movies, some people will use it to listen to music while playing games, some people will use it while listening to music while reading documents, the edge of QQ.

This computer can be abstracted into a class computer, there is a method dowork, all people have to do their own things through this method.

We can implement some fixed things without a delegate, not flexible enough:

For example:

The code is as follows:

Class Program {

static void Main (string[] args) {

Computer computer = new computer ();

Computer. DoWork ("Pig", "watching a movie!" ”);

Computer. DoWork ("The Sand Monk", "in writing the program!") ”);

Computer. DoWork ("Monkey King", "playing the game!") ”);



public class Computer {

public void DoWork (string name, string work) {

Console.WriteLine ("{0}{1}", name, work);



In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Delegate, event Learning notes in C #

This address:

Related content examples of pirate split-gold algorithm implemented by C # use WebClient to log in to the Web site and crawl the login Web page information implementation Method C # in the use of string StringBuilder StringBuffer Class C # webclient implement file download
Example of a queue (Quene) for C # Data structures Introduction to the beginning of self-study of C # Programming introduce partial methods and partial class analysis in C # WinForm implementing methods for capturing forms to minimize, maximize, and Close button events

Delegate, event Learning notes in C #

Related 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: 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.