C # Delegate series (ii) bind methods to delegate

Source: Internet
Author: User
Bind method to delegate

Through the example of (1), is there a dream? So, are you wondering: In the above example, I don't have to assign a value to the name parameter directly in the greetpeople () method. I can use the variable like this:

Static void main (string [] ARGs ){
String name1, name2;
Name1 ="Jimmy";
Name2 ="Half smoke";

Greetpeople (name1, englishgreeting );
Greetpeople (name2, chinesegreeting );
Console. readkey ();
}

Since the same status of the delegate greetingdelegate and the type string is defined as a parameter type, can I use the delegate as well?

Static void main (string [] ARGs ){
Greetingdelegate delegate1, delegate2;
Delegate1 = englishgreeting;
Delegate2 = chinesegreeting;

Greetpeople ("Jimmy", Delegate1 );
Greetpeople ("Half smoke", Delegate2 );
Console. readkey ();
}

As expected, this is no problem,ProgramOutput as expected. Here, I want to talk about the Delegate Feature different from the string feature: You can assign multiple methods to the same delegate, or bind multiple methods to the same delegate, when this delegate is called, the bound methods are called in sequence. In this example, the syntax is as follows:

Static void main (string [] ARGs ){
Greetingdelegate delegate1;
Delegate1 = englishgreeting ;//Assign values to the delegate type variables first.
Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

//Will be called successivelyEnglishgreetingAndChinesegreetingMethod
Greetpeople ("Jimmy", Delegate1 );
Console. readkey ();

}

Output:
Morning,Jimmy
Good morning,Jimmy

In fact, we can also bypass the greetpeople method and directly call englishgreeting and chinesegreeting through delegation:

Static void main (string [] ARGs ){
Greetingdelegate delegate1;
Delegate1 = englishgreeting ;//Assign values to the delegate type variables first.
Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

//Will be called successivelyEnglishgreetingAndChinesegreetingMethod
Delegate1 ("Jimmy");
Console. readkey ();
}

Note:This is no problem in this example, but looking back at the above definition of greetpeople (), we can do some work that needs to be done for both engshihgreeting and chinesegreeting, I omitted it for convenience.

Note that "=" is used for the first time, which is the syntax of value assignment; "+ =" is used for the second time, which is the binding syntax. If "+ =" is used for the first time, the compilation error "using unassigned local variables" will occur.

We can also use the followingCodeTo simplify this process:

Greetingdelegate delegate1 = new greetingdelegate (englishgreeting );

Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

As you can see, it should be noted that the first statement of this Code is similar to instantiating a class. You can't help but think that the "+ =" Compilation error cannot be used when the delegate is bound for the first time, this method may be used to avoid:

Greetingdelegate delegate1 = new greetingdelegate ();

Delegate1 + = englishgreeting ;//This time we use "+ =", Binding syntax.

Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

But in fact, there will be a compilation error: the "greetingdelegate" method does not use the "0" parameter overload. Although this result is a bit frustrating, the compilation prompt "no overload of 0 Parameters" reminds us of the class constructor again. I know that you may not be able to find out what it is, but before that, we need to finish introducing basic knowledge and applications.

Since a delegate can bind a method, there should be a way to unbind the method. It is easy to think that this syntax is "-= ":

Static void main (string [] ARGs ){

Greetingdelegate delegate1 = new greetingdelegate (englishgreeting );

Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

//Will be called successivelyEnglishgreetingAndChinesegreetingMethod

Greetpeople ("Jimmy", Delegate1 );

Console. writeline ();

Delegate1-= englishgreeting ;//CancelEnglishgreetingMethod binding

//Will only callChinesegreeting

Greetpeople ("Half smoke", Delegate1 );

Console. readkey ();

}

Output:

Morning,Jimmy

Good morning,Jimmy

Good morning,Half smoke

Let's summarize the delegation again:

You can use a delegate to bind multiple methods to the same delegate variable. When you call this variable (the word "call" is used here because this variable represents a method ), you can call all bound methods in sequence.

From: http://hi.baidu.com/txh1204Bind method to delegate

Through the example of (1), is there a dream? So, are you wondering: In the above example, I don't have to assign a value to the name parameter directly in the greetpeople () method. I can use the variable like this:

Static void main (string [] ARGs ){
String name1, name2;
Name1 ="Jimmy";
Name2 ="Half smoke";

Greetpeople (name1, englishgreeting );
Greetpeople (name2, chinesegreeting );
Console. readkey ();
}

Since the same status of the delegate greetingdelegate and the type string is defined as a parameter type, can I use the delegate as well?

Static void main (string [] ARGs ){
Greetingdelegate delegate1, delegate2;
Delegate1 = englishgreeting;
Delegate2 = chinesegreeting;

Greetpeople ("Jimmy", Delegate1 );
Greetpeople ("Half smoke", Delegate2 );
Console. readkey ();
}

As expected, this is fine, and the program is output as expected. Here, I want to talk about the Delegate Feature different from the string feature: You can assign multiple methods to the same delegate, or bind multiple methods to the same delegate, when this delegate is called, the bound methods are called in sequence. In this example, the syntax is as follows:

Static void main (string [] ARGs ){
Greetingdelegate delegate1;
Delegate1 = englishgreeting ;//Assign values to the delegate type variables first.
Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

//Will be called successivelyEnglishgreetingAndChinesegreetingMethod
Greetpeople ("Jimmy", Delegate1 );
Console. readkey ();

}

Output:
Morning,Jimmy
Good morning,Jimmy

In fact, we can also bypass the greetpeople method and directly call englishgreeting and chinesegreeting through delegation:

Static void main (string [] ARGs ){
Greetingdelegate delegate1;
Delegate1 = englishgreeting ;//Assign values to the delegate type variables first.
Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

//Will be called successivelyEnglishgreetingAndChinesegreetingMethod
Delegate1 ("Jimmy");
Console. readkey ();
}

Note:This is no problem in this example, but looking back at the above definition of greetpeople (), we can do some work that needs to be done for both engshihgreeting and chinesegreeting, I omitted it for convenience.

Note that "=" is used for the first time, which is the syntax of value assignment; "+ =" is used for the second time, which is the binding syntax. If "+ =" is used for the first time, the compilation error "using unassigned local variables" will occur.

We can also use the following code to simplify this process:

Greetingdelegate delegate1 = new greetingdelegate (englishgreeting );

Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

As you can see, it should be noted that the first statement of this Code is similar to instantiating a class. You can't help but think that the "+ =" Compilation error cannot be used when the delegate is bound for the first time, this method may be used to avoid:

Greetingdelegate delegate1 = new greetingdelegate ();

Delegate1 + = englishgreeting ;//This time we use "+ =", Binding syntax.

Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

But in fact, there will be a compilation error: the "greetingdelegate" method does not use the "0" parameter overload. Although this result is a bit frustrating, the compilation prompt "no overload of 0 Parameters" reminds us of the class constructor again. I know that you may not be able to find out what it is, but before that, we need to finish introducing basic knowledge and applications.

Since a delegate can bind a method, there should be a way to unbind the method. It is easy to think that this syntax is "-= ":

Static void main (string [] ARGs ){

Greetingdelegate delegate1 = new greetingdelegate (englishgreeting );

Delegate1 + = chinesegreeting ;//Bind the delegate variable to another method.

//Will be called successivelyEnglishgreetingAndChinesegreetingMethod

Greetpeople ("Jimmy", Delegate1 );

Console. writeline ();

Delegate1-= englishgreeting ;//CancelEnglishgreetingMethod binding

//Will only callChinesegreeting

Greetpeople ("Half smoke", Delegate1 );

Console. readkey ();

}

Output:

Morning,Jimmy

Good morning,Jimmy

Good morning,Half smoke

Let's summarize the delegation again:

You can use a delegate to bind multiple methods to the same delegate variable. When you call this variable (the word "call" is used here because this variable represents a method ), you can call all bound methods in sequence.

From: http://hi.baidu.com/txh1204

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.