C #3.0 Study Notes (7) Delegation

Source: Internet
Author: User

 

1. What is delegation?

A: A delegate is a list of ordered methods with the same signature and return value types.

Note:

1> the method list is called the invocation list ).

2> when a delegate is called, each method in the method list is called.

3> the delegate is a reference type data, so there are references and objects.

2. How do I declare a delegate?

Delegate void MyDel (int x );

Note:

1> delegate is a keyword.

2> void indicates the return type. If a return value exists, it indicates the corresponding return type, such as int.

3> MyDel indicates the delegate name.

4> MyDel (int x) indicates the signature.

3. How can I create a delegate object?

Method 1: use an object with the new operator to create an expression.

MyDel del = new MyDel (ClassA. method1 );

Method 2: Use the shortcut syntax, which consists of only the class name and method name. The shortcut syntax is used because there is implicit conversion between the method name and the corresponding delegate type.

MyDel del = new ClassA. method1;

4. Delegate call?

1> delegate calls without return values and parameters:

Namespace delegate1

 

{

 

/* Delegate with no return value or parameter */

 

Delegate void PrintFunction (); // defines a delegate type without return values or numeric values.

 

Class Program

 

{

 

Static voidMain (string [] args)

 

{

 

Test t = new Test ();

 

PrintFunction pf; // creates an empty delegate object

 

Pf = t. Print1; // initialize the delegate

 

Pf + = Test. Print2; // use the + = Operator to add a method to the delegate. Pay attention to the usage (class name. method name) of the aggregate method ).

 

Pf + = t. Print1;

 

Pf + = Test. Print2;

 

If (pf! = Null) // determine whether the delegate contains a method

 

{

 

Pf (); // Delegation

 

}

 

Else

 

{

 

Console. WriteLine ("there is no way in the delegate! ");

 

}

 

Console. ReadKey ();

 

}

 

}

 

Class Test

 

{

 

Public void Print1 ()

 

{

 

Console. WriteLine ("Print1 --- real-sample method ");

 

}

 

Public static void Print2 ()

 

{

 

Console. WriteLine ("Print2 --- publish succeeded method ");

 

}

 

}

 

}

The program output result is:

 

 

2> call with return value delegate:

Namespace delegate2

 

{

 

/* Delegate with return value */

 

/*

 

1. The value returned by the last method in the delegate method list is the value returned by the delegate method.

 

2. The returned values of all other methods in the method list are ignored.

 

*/

 

Delegate int Mydel ();

 

Class Program

 

{

 

Static voidMain (string [] args)

 

{

 

MyClass mc = new MyClass ();

 

Mydel mDel = mc. Add2;

 

MDel + = mc. Add3;

 

MDel + = mc. Add2;

 

Console. WriteLine ("Value: {0}", mDel ());

 

Console. ReadKey ();

 

}

 

}

 

Class MyClass

 

{

 

Int IntValue = 5;

 

Public int Add2 ()

 

{

 

IntValue + = 2;

 

Return IntValue;

 

}

 

Public int Add3 ()

 

{

 

IntValue + = 3;

 

Return IntValue;

 

}

 

}

 

}

The output result of the program is:

 

 

3> call of a delegate with reference parameters:

Namespace delegate3

 

{

 

/* Delegate with reference to the serial number */

 

/*

 

1. When the delegate list is used for the next method, the new value (not the initial value) of the delegate number is routed to the next method.

 

*/

 

Delegate void MyDel (ref int x );

 

Class Program

 

{

 

Static voidMain (string [] args)

 

{

 

MyClass mc = new MyClass ();

 

MyDel mDel = mc. Add2; // Add2 (x = 5)

 

MDel + = mc. Add3; // Add3 (x = 7)

 

MDel + = mc. Add2; // Add2 (x = 10)

 

Int x = 5;

 

MDel (ref x );

 

Console. WriteLine ("delegate mDel value: {0}", x );

 

Console. ReadKey ();

 

}

 

}

 

Class MyClass

 

{

 

Public void Add2 (ref int x)

 

{

 

X + = 2;

 

}

 

Public void Add3 (ref int x)

 

{

 

X + = 3;

 

}

 

}

 

}

The output result of the program is:

 

 

4> anonymous methods and lambda expressions:

Anonymous method definition: An anonymous method is an inline (inline) declaration method during delegate initialization.

Namespace delegate4

 

{

 

/* Anonymous method and lambda table naming */

 

Delegate double MyDel (int par );

 

Class Program

 

{

 

Static voidMain (string [] args)

 

{

 

MyDel del = delegate (int x) {return x + 1 ;}; // anonymous method.

 

MyDel le1 = (int x) =>{ return x + 1 ;}; // lambda table alias type. Keyword delegate is omitted.

 

MyDel le2 = (x) =>{ return x + 1 ;}; // lambda expression. The type parameter is omitted because the compiler can know the type of the delegate parameter from the delegate declaration.

 

MyDel le3 = x =>{ return x + 1 ;}; // lambda expression. Parentheses are omitted because there is only one implicit type parameter.

 

MyDel le4 = x => x + 1; // lambda expression. Omit statement blocks.

 

Console. WriteLine ("delegate del value: {0}", del (12 ));

 

Console. WriteLine ("delegate le1 value: {0}", le1 (12 ));

 

Console. WriteLine ("delegate le2 value: {0}", le2 (12 ));

 

Console. WriteLine ("delegate le3 value: {0}", le3 (12 ));

 

Console. WriteLine ("delegate le4 value: {0}", le4 (12 ));

 

Console. ReadKey ();

 

}

 

}

 

}

The program output result is:

 

 

The above is my understanding and summary of the delegation. If you think the summary is good, please give us some encouragement. If you think there are still some shortcomings, you are also welcome to testify. Let's work together, come on!

 

 



The author's wheat forever.

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.