C # Delegate and event in basic series-C # (1)

Source: Internet
Author: User
We all know that C # has the concept of "interface". The so-called "interface" is to define a set of standards and then implement the methods in the implementation class. Therefore, "interface, is the abstraction of a group of classes ". In the same way, we can regard "delegate" as "method abstraction", that is, to define a method template. As for how this method is specific, it is implemented by the method itself.

We know that the biggest benefit of an interface is that it can realize polymorphism. Similarly, "delegate" can realize the polymorphism of methods. When we want to call a specific method, we do not directly call this method, instead, call the delegate. Of course, we must establish an association between the specific method and the delegate.

There are three steps to define and use delegation:

1. Delegate statement. 2. Delegate instantiation. 3. Delegate call.

I. Define Delegation

Syntax for defining delegation:

<Access modifier> delegate name of the delegate return type ();

The definition delegate is similar to the definition method. The delegate does not have a specific implementation body. It is declared by the keyword delegate and ended directly with a semicolon. The method that a delegate can represent is determined by its return value type and parameter list. The following is a delegate definition:

Public Delegate void mydelegate (string name );

The mydelegate delegate can only represent a method with no return value and the parameter is a string.

Ii. instantiate Delegation

After a delegate is defined, how can I use the delegate representative method for execution? First, instantiate the delegate. Instantiate a delegate is to direct it to a method, that is, to call the Delegate constructor and pass the associated methods as parameters.

For example, a method
Private int add (INT num1, int num2)
{
Return (num1 + num2 );
}

After the delegate is declared, it can be instantiated like a class. During the instantiation, the referenced method (such as: add) is used as a parameter, so that the delegate and method are associated, you can use the delegate to reference the method.
The delegate and the referenced method must be consistent:
1. The number, type, and order of parameters must be completely consistent.
2. The return values must be consistent.
3. The input method does not contain parameters. Only the method name is required.

Iii. Call Delegation

To call the delegate method, add parameters to the delegate object. If the parameter is empty, add parentheses.

Creating a delegate for a method and then calling the method immediately through this delegate does not make much sense, because directly calling the method is simpler. When a method is called dynamically, the delegate displays its utility.

Using system;

Delegate int operand (int A, int B); // Step 1: Delegate statement
Class class1
{
Static void main (string [] ARGs)
{
Class1 C1 = new class1 ();
Operand ope = new operand (c1.add );
// Delegate instantiation. Note that the parameter is the name of the parameter to be used without parentheses.
Console. writeline (OPE (10, 20); // call the delegate Method

Delegate object and Parameters
Console. Readline ();
}
// Define a method to calculate the sum of two Addons
Private int add (INT num1, int num2)
{
Return (num1 + num2 );
}
}

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.