What is a delegate in C # and whether the event is a delegate

Source: Internet
Author: User

A delegate is a type that holds a reference to a method in an object and is also a type-safe function pointer. Give you an example://declares a delegate, where NewDelegate () is understood to be a type that is then used to fetch the method.  Delegate voidnewdelegate ();//The following is a two-way definition of a, B, which you can understand as a newdelegate of two instances of this type.   Public voidA () {}; Public voidB () {}; Public voidMain () {newdelegate DGT1=NewNewDelegate (a); NewDelegate DGT2=NewNewDelegate (b);//DGT1 points to method a,dgt2 points to method B. That is, the return value of a method and the number of parameter types are declared with the delegate//match, you can invoke this method with a delegate. And the name of this method is passed as a parameter to the instantiated delegate.} event is a special kind of delegate, why do you say so? By deserializing the. NET Framework with the Anti-compilation tool, you can discover the definition of the event (this is just the definition of one of the events, the arguments for the event are defined differently in the class library), as follows: Public Delegate voidEventHandler (Objectsender, EventArgs e); And how do the click events of the various controls that are common are defined?  Public EventEventHandler Click; Note The Click event is a delegate and one of our common button controls adds an event. Pageload: Button1.Click+=NewEventHandler (button1_click); Define events for Button1protectedButton1_Click (ObjectSender,eventargs e) {} As you can see, this is a typical delegate invocation. 

What is a delegate in C # and whether the event is a delegate

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