In one sentence, let's clarify what delegate is, And let's clarify delegate.

Source: Internet
Author: User

In one sentence, let's clarify what delegate is, And let's clarify delegate.
A delegate is a class object call. A delegate object represents any class object that implements the delegate.
What is Delegate!

What is Delegate?
For Microsoft, a delegate statement specifies a related type. You can use this type to encapsulate a method containing a specific tag. A delegate instance can encapsulate a static or instance method. In summary, Delegates is like a function pointer in C ++. However, delegates are safe and secure.

My interviews are conducted by phone, so I can easily and easily elaborate on these definitions. Transparent and detailed explanation is necessary. However, the definition of delegates is that some methods can be used at any time even if you have no knowledge about the target object. Such a method is delegates. Many developers usually call them callback functions ). Now let's look at an example to better understand its application.

Delegates in use
Our sample program starts with two simple C # classes. These two classes contain a method to display information through the console:

Public class DelegateClass1 {

Public void exampleMethod1 (String passedString ){

Console. WriteLine (passedString );}

}

Public class DelegateClass2 {

Public void exampleMethod2 (String passedString ){

Console. WriteLine (passedString );}

}

Note that these two classes perform similar methods. These methods have different names, but they have the same tag and entity. You can use a delegate to combine these two methods. The implementation process is to abstract a new data type from the common features of these two methods. That is, they all take a simple character as a variable and return void. C # delegate keywords make these possible:

Public delegate void example (String passedString );

For example, using this new data type declaration, we can call these two methods through a common abstraction. The following C # console program uses our delegate example to call the methods of these two classes:

Public class TestExampleDelegate {

Public delegate void example (String passedString );

Public static void Main (string [] args ){

Example [] items = new example [2];

Items [0] = new example (new DelegateClass1 (). exampleMethod1 );

Items [1] = new example (new DelegateClass2 (). exampleMethod2 );

For (int I = 0; I <items. Length; I ++ ){

Items [I] ("Builder.com Rules! ");

}}}

The code above creates an array of sample objects and instantiates these objects using methods in these classes. Because parentheses are ignored, the sample object is processed by a call method. The code execution result is displayed in the text.

(Note: Delegates do not belong to the details of C #. They are standard parts of the. NET Framework, so you can use other languages, such as VB. NET or J #.)

Delegates can be used anywhere
At this point, you may only know this thing, but you are not aware of it in your... the rest of the text>

Which of the following is the Delegate Methods (delegation method) in eclipse automatic annotation settings?

Method label:
/**
* $ {Tags}
* $ {See_to_target}
*/
Reference: www.chineselinuxuniversity.net/articles/28810.shtml

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.