C # delegates and events

Source: Internet
Author: User

Commissioned

Defining a delegate's syntax and definition method is similar, just a keyword more than the method delegate, we all know that the method is to parameterize the type, so-called type parameterization means that the method takes a parameter, which is a parameter of a certain type, such as int, string, etc., and the delegate is the party Method Parameterization, said that the above type of parameterization, I believe you can also guess the meaning of the method parameterization, yes, is to pass the method as a parameter to a delegate.

First look at the statement that declares the delegate:

Public deletate void MyDelegate ();

Public: Access modifier delegate: keyword void: return type mydelegate: Delegate name (): Parameter list

See the Declaration people will think about why the return value of the delegate, parameter list so I can't return a string, an int? Can I add a few parameters to the delegate? The answer is: Of course, but the definition of the delegate is relative to the method, because your delegate is ultimately to register the method, and your method has some kind of signature, so you have to give a method of how to sign a delegate, the delegate will have the same signature as the method, it is similar to you with an int Type of variable to accept a string type of value, obviously not (personal understanding) ....

* Entrusted as long as the definition is possible, we do not need to care about his implementation

Use of Delegates

There are two ways to register a delegate:

First: Assign the method directly [=] or use "+ =" to give a delegate ==> delegate name =[+=] Method name

The second: The nature of the delegate is also a class, just a special class, so we can also instantiate a delegate object through the delegate constructor to register the delegate = = "Delegate Name object name = new delegate name (method name )

C # delegates and events

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.