Book notes on. NET4.0 Object-Oriented Programming-Chapter 1 magic Delegation

Source: Internet
Author: User

Chapter 2 magic Delegation

 

[Description]: In. NET, event-driven, asynchronous calling, and Lambda are all built on delegation. This chapter describes the basic knowledge of delegation, generic delegation, Lambda, and method callback.

 

Section 1 concept and use of delegation

1. Concept: delegation can be considered as a data type and can be used to define variables. However, the defined variable can receive only one method.

2. instance:

Public class MathOpt

{

Public int Add (int argument1, int argument2)

{

Return argument1 + argument2;

}

}

Public delegate int MathOptDelegate (int value1, int value2); // defines a delegate type.

MathOptDelegate optDel; // defines a delegate variable.

MathOpt obj = new MathOpt ();

OppDel = obj. Add; // Method for receiving an object instance

Console. WriteLine (oppDel (1, 2); // method call

3. Method Signature: when defining the delegate type, the method requirement is called the method Signature. It specifies the number and type of parameters of the method and the return value of the method.

4. Basic delegate usage:

A. Use the delegate dynamic call method:

For example, void DoCalculate (CalculateDelegate clmethod) // The input parameter is a variable of the delegate type.

B. implement multi-form communication using delegation: Implement information communication between different objects.

One row in the book is incorrect: frm. recorder = this. ShowCount is changed to frm. recorder = this. Record.

 

Section 2: in-depth exploration of delegated Technology

1. essence: A delegate is essentially a class that inherits from the System. MulticastDelete class and maintains a list of delegates with links.

2. When a multicast delegate is called, it is called in the order of the delegate list in the delegate list.

3. It inherits three important methods: BeginInvoke, EndInvoke, and Invoke.

4. Once a delegate is created, its delegated call list cannot be changed.

 

 

Section 3 use generic delegation

1. Concept: generic delegation is similar to common delegation. The difference is that you need to specify generic parameters when using generic delegation. For example

MyGenericDelegate <int> del = MyFunc;

2. Entrust the Func system. There are multiple overload forms based on the number of generic parameters. The Func series delegate is used to reference a method with a returned value.

For example, public delegate TResult Func <TResult> ()

Public delegate TResult Func <T, TResult> (T arg)

Public delegate TResult Func <T1, T2, TResult> (T1 arg, T2 arg)

3. Action System delegate. There are multiple overload forms based on the number of generic parameters. It is used to return the void method.

For example, public delegate void Action ()

Public delegate void Action <T> (T obj)

Public delegate void Action <T1, T2> (T1 arg1, T2 arg2)

4. Delegate Predicate <T>.

A. Define public delegate bool Predicate <T> (T obj );

B. It references a method that returns the bool value. In actual use, it often uses its variable to reference a "Conditional judgment function ".

C. Example: List <T> has a Find method. Public T Find (Predicate <T> match );

 

 

Section 4 anonymous methods and Lambda expressions

1. Anonymous method:

A. instance: public delegate int AddDelegate (int I, int j );

AddDelegate del = delegate (int I, int j)

{

Return I + j;

};

Console. WriteLine (del (100,200 ));

B. The anonymous method combines the method definition with the assignment of the delegate variable, saving the trouble of defining a method separately.

2. Lambda expressions:

A. Example: SomeDelegateType del2 = arguments =>{ return arguments. ToString ();};

B. Lambda expressions are actually further simplified by the anonymous method. They can be used to define an anonymous function and pass it to a delegate variable.

C. Usage:

C.1 when there is only one input parameter, parentheses are optional. If there is only one return statement in an expression, the return keyword can be omitted.

Func <int, bool> del1 = (x) => {return x> 0 ;}

Func <int, bool> del1 = x => x> 0;

C.2 when two or more input parameters are separated by commas.

Func <int, int, bool> del2 = (x, y) => x = y;

 

Section 5 skills in callback Programming

Callback is a very useful programming technology. This section describes four typical callback methods in. NET applications.

1. Implementation Scheme of interface-based callback:

2. Implement callback using delegation.

3. timed callback.

Using the Time class, you can call back a method that meets the requirements of the TimerCallBack delegate.

Time class constructor public Timer (TimerCallback callback, object state, int dueTime, int period)

Among them: callback: The method for timed callback.

State: the parameter transmitted to the callback method.

Duetime: The amount of time before the callback method is called (in milliseconds ).

Period: the number of milliseconds to call the callback method.

4. multi-thread callback:

 

 

Summary: Delegation is essentially a class that implements an object-oriented, type-safe method callback mechanism.

 

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.