[Translation] using delegates in C #

Source: Internet
Author: User

Introduction
The delegate in C # is like the function pointer in C/C ++. one multi-delegate can subscribe to multiple methods. A delegate can be used to call a function. The specific function called is determined during running.

What is delegation? Why do they need them?

The delegate is type-safe in C #. You can subscribe to one or more function pointers with the same signature method. the delegate is a reference type in C. the delegate must have the same signature as the method pointed.

C # There is a delegate class in the system namespace, which provides support for delegation. There are two types of delegation: single delegation and multiple delegation.
A single delegate can only subscribe to one method, while multiple delegates can subscribe to multiple methods with the same signature.

The signature type of a delegate consists of the following aspects:
1. Delegate name
2. parameters accepted by the delegate
3. Delegate return type

If there is no modifier, the delegate is either public or internal. The following is an example of declaring the delegate.

Declare a delegate

Public Delegate void testdelegate (string message );

 

In the preceding example, the return type of the delegate is void, which receives a string-type parameter. you can subscribe to and call a method with the same signature as him. A delegate must be instantiated before use.

The following statement instantiate a delegate. testdelegate T = new testdelegate (Display );

Implement a single delegate using system;
Public Delegate void testdelegate (string message); // declare the delegate
Class Test
...{
Public static void display (string message)
...{
Console. writeline ("the string entered is:" + message );
}
Static void main ()
...{
Testdelegate T = new testdelegate (Display); // instantiate the delegate
Console. writeline ("Please enter a string ");
String message = console. Readline ();
T (Message); // invoke the delegate
Console. Readline ();
}
}

Implement a multi-delegate using system;
Public Delegate void testdelegate ();
Class Test
...{
Public static void display1 ()
...{
Console. writeline ("This is the first method ");
}
Public static void display2 ()
...{
Console. writeline ("this is the second method ");
}
Static void main ()
...{
Testdelegate T1 = new testdelegate (display1 );
Testdelegate t2 = new testdelegate (display2 );
T1 = t1 + T2; // make T1 a multi-cast delegate
T1 (); // invoke delegate
Console. Readline ();
}
}

Created by jecray

Execute the above Code and it will be shown as follows:
This is the first method
This is the second method

Delegation can also be used with events. This article only briefly introduces delegate.

Original article: http://aspalliance.com/1228_Working_with_Delegates_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: 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.