C #1 core infrastructure (I)-delegated

Source: Internet
Author: User

Delegation provides indirect methods to some extent. In other words, you do not need to specify an action directly. Instead, you can "include" this action in some way in an object. This object can be used as any other object. The encapsulated behavior can be executed in this object. You can regard the delegate type as an interface that defines only one method, and the delegated instance as an object that implements that interface.

Four basic conditions for delegation:

1) Declare the delegate type;

2) A method must contain the code to be executed;

3) You must create a delegated instance;

4) The instance must be called (invoke.

 

1. Declare the delegate type

The delegate type is actually a list of parameter types and a return type. It specifies the actions that can be represented by type instances. Declare a delegate type as follows.

Delegate void StringProcessor (string input );

The point here is that StringProcessor is actually a type. You can create an instance and pass the reference to the instance.

2. Find an appropriate method for delegated instance operations

Find (or write) a method that can do what we want and has the same signature as the delegate type.

Let's take a look at the five alternative method signatures of the following StringProcessor instance:

Void PrintString (string x)

Void PrintInteger (int x)

Void PrintTwoString (string x, string y)

Int GetStringLength (string x)

Void PrintObject (object x)

We can see that the first method fully meets the requirements, so you can use it to create a delegated instance. The second method has a parameter but is not of the string type, so it is not compatible with StringProcessor. The third method parameter quantity does not match, so it is not compatible. The fourth method has a correct parameter list, but the return type is not void (if the delegate type has a return type, the return type of the method must also match ). The fifth method is interesting. Any time you call a StringProcessor instance, you can call the PrintObject method with the same parameters, because string is derived from the object.

3. Create a delegated instance

Now that you have a delegate type and a method with a correct signature, you can create an instance of the delegate type and specify to execute this method when calling the delegate instance. The expressions used to create a delegated instance depend on the instance method or static method used for the operation. If the operation is a static method, you can specify the type name. If the instance method is used during the operation, you must first create an instance of the type (or its derived type. This is the same as the call method.

Assume that PrintString is a static method in the StaticMethods type and is an instance method in the InstanceMethods type. The following are two examples of creating a StringProcessor instance:

StringProcessor proc1, proc2;

Proc1 = new StringProcessor (StaticMethods. PrintString );

InstanceMethods instance = new InstanceMethods ();

Prce2 = new StringProcessor (instance. PrintString );

4. Call the delegated instance

This is simple (for Synchronous calls only)-you can call the method of a delegate instance. This method is called Invoke. In the delegate type, this method appears in the form of a delegate type, and has the same parameter list and return type as specified in the delegate type declaration. The call to Invoke will perform the operations on the delegated instance and pass it any parameters specified when the Invoke is called. Let's have a better understanding:

 

Here is a complete code:

Using System; // declare the delegate type delegate void StringProcessor (string input); class Person {string name; public Person (string name) {this. name = name;} // declare the compatible instance method public void Say (string message) {Console. writeLine ("{0} said: {1}", name, message) ;}} class Background {// declare a compatible static method public static void Note (string note) {Console. writeLine ("({0})", note) ;}} class SimpleDelegateUse {static void Main () {Person jon = new Person ("Jon"); Person Tom = new Person ("Tom"); // create three delegate instances: StringProcessor jonsVoice, tomsVoice, background; jonsVoice = new StringProcessor (jon. say); // instance method tomsVoice = new StringProcessor (Tom. say); // instance method background = new StringProcessor (Background. note); // static method // call the delegate instance jonsVoice ("Hello, World! "); TomsVoice. Invoke (" Welcome to my blog! "); Background (" All Rights Reserved! "); Console. Read ();}}

Effect

 

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.