Asp. NET Simple Series 4-also talk about delegates and events

Source: Internet
Author: User

I. The definition of a delegate:

"Advanced Chinese Dictionary" is explained as follows: entrusted to other people or institutions to deal with. To say that the meaning of life in fact everyone can understand, is nothing but "when someone (organization) needs to complete a thing that they can not or should not be done, the person (organization) is looking for a suitable person who is capable of accomplishing this, and then provides the necessary information to entrust the matter to the identified person (institution) for completion. "A delegate in C # is a type of reference method, and once a method is assigned to a delegate, the delegate behaves exactly like the method, and the use of the delegate method can have parameters and return values as any other method." A delegate object can be passed to code that invokes the method reference without knowing which method will be invoked at compile time. A delegate is the encapsulation of a function, which represents a "class" function. They all meet a certain signature: they have the same argument list, return value type. Delegates can also be viewed as abstractions of functions, which are "classes" of functions. At this point, the delegate instance represents a specific function. A delegate should be a level of similarity, and it is very much like a class. Let's take a look at an instance of a delegate using:

public delegate void Printhandler (String str);      Declare delegate type public
class Printstr
{public
void Callprint (string input)
{
Console.WriteLine (input);
}
static void Main (string[] args)
{
Printstr myprinter = new Printstr ();
Printhandler MyHandler = null;
The delegate is linked to a method to instantiate the delegate
MyHandler + = new Printhandler (myprinter.callprint);
if (MyHandler!= null)
MyHandler ("Hello world!");      Invokes the delegate, which is equivalent to the method linked by the anonymous invocation delegate
MyHandler-= new Printhandler (myprinter.callprint);
if (MyHandler = = null)
Console.WriteLine ("Myhandler==null");
Console.read ();
}        

The results obtained are

Hello world!

Myhandler==null

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.