Delegate in. net

Source: Internet
Author: User

1. What is delegation?

The format of the delegate type declaration is as follows:

// Declarative Delegation
Public   Delegate   Void Testdelegate ( String Message );

1. DelegateA keyword is used to declare a reference type. The reference type can be used to encapsulate naming or anonymous methods. Delegation is safe and reliable.

2. delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. The use of delegate methods can have parameters and return values like any other method, as shown in the following example:

3. Delegation has the following features:

    • The delegate is similar to a C ++ function pointer, but it is type-safe.

    • The delegate allows passing methods as parameters.

    • A delegate can be used to define a callback method.

    • The delegate can be linked together. For example, multiple methods can be called for an event.

    • The method does not need to be exactly matched with the delegate signature. For more information, see covariant and inverter.

    • C #2.0 introduces the concept of anonymous methods, which allowCodeBlock is passed as a parameter to replace the separately defined method.

Ii. Several Methods of delegate writing in. net

1. The statement in. NET 1.0 is as follows:

View code

  // Declarative delegate Signature
  Public   Delegate   Void Testdelegate ( String Message );
// Delegate Matching Method
Public   Static   Void Testmethod ( String MSG)
{
Console. writeline ( String . Format ( " . Net delegate: {0} " , MSG ));
}

Static   Void Main ( String [] ARGs)
{
# Region . Net 1. x
Testdelegate dele1 = Testmethod;
Dele1 ( " . Net 1. x " );

Testdelegate dele2= NewTestdelegate (testmethod );
Dele2 (". Net 1. x");
# Endregion
}

2. In. NET 2.0,. NET 2.0 introducesAnonymous method,You can also use the. net1.0 method.

View code # Region . Net2.x
Static   Void Main ( String [] ARGs)
{
// Declaring the implementation of a principal object directly directs to a method (anonymous ).
Testdelegate dele3 =   Delegate ( String MSG)
{
Console. writeline ( String . Format ( " . Net delegate: {0} " , MSG ));
};
Dele3 ( " . Net2.x Writing Method-anonymous method " );
}

3. In. NET 3. X, introduceLambda expressions,Let's take a look at Microsoft's Lambda expression definition:

"Lambda expression" is an anonymous function.It can contain expressions and statements and can be used to create a delegate or expression tree type.

All lambda expressions use the lambda operator =>, which reads "goes ".The left side of the lambda operator is the input parameter (if any), and the right side contains the expression or statement block.Lambda expressionsX => X * xRead as "X goes to X times X ".

View code

// The left side of the lambda operator is the input parameter (if any), and the right side contains the expression or statement block.
Testdelegate dele4 = MSG => Console. writeline ( String . Format ( " . Net delegate: {0} " , MSG ));
Dele4 ( " Lambda expressions can be introduced in the. NET 3. x write method. " );

Iii. Delegated application scenarios:

Only when a single method is called.
When a class requires multiple executions described by methods.
When the static method is expected to be used to execute the specification.
Expected to get a similar event mode.
When the caller does not need to know the object that does not need to obtain the definition method
Only when the specification is distributed to a few established components.
When you want a simple structure.

 

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.