One of the core foundations of C # 1--delegation

Source: Internet
Author: User

One of the core foundations of C # 1--delegation

One of the core foundations of C # 1--delegation
The composition of a simple delegate
Merging and deleting delegates
Simple discussion of events
Commission Summary

The composition of a simple delegate
  1. Declaring the delegate type ;
    delegate void StringProcessor(string input);

  2. Find an appropriate method for the operation of the delegate instance
    void PrintString(string x)Fully meet the requirements
    void PrintInteger(int x)Parameter type is not compatible
    void PrintTwoStrings(string x, string y)The number of arguments does not match
    int GetStringLength(string x)return type mismatch
    void PrintObject(object x)C # 1 requires exactly the same parameter type, so although string is derived from object, it still does not match. But C # 2 improved the situation.
    As with parameter types, in, out, and ref prefixes must also match, although rarely used.

  3. Create a delegate instance
    The specific form of expression used to create a delegate instance depends on whether the operation uses an instance method or a static method.

        
       
    1. StringProcessor proc1, proc2;
    2. proc1 = new StringProcessor(StaticMethods.PrintString);// 静态方法
    3. InstanceMethod instance = new InstanceMethod();
    4. proc2 = new StringProcessor(instance.PrintString);//实例方法
  4. Invoking a delegate instance

    Handles calls to delegate instances that use the C # simplified syntax.

The essence of a delegate is to perform some kind of operation indirectly .

Merging and deleting delegates

The delegate instance actually has an action list called the invocation list of the delegate instance.
System.DelegateA static method of type Combine and Remove is responsible for creating a new delegate instance. Rarely called directly, generally using +/+= and-/-= operators.
When a delegate instance is invoked, all its operations are executed sequentially, and the return value of Invoke is the return value of the last operation. If any action in the invocation list throws an exception, subsequent operations are prevented.

Simple discussion of events

The event is not a field of a delegate type. Events can be seen as similar to attributes (essentially, methods).

    1. Both are declared to have a specific type, and the event must be a delegate type.
    2. The essence of a property is the invocation of a value method and an assignment method. Similarly, when subscribing to or canceling an event, it looks like a field of the delegate type is used with the + = and-= operators, and the add and remove methods are actually called.
    3. The first reason for the existence of an "event" is similar to the "attribute"--adding a wrapper layer to implement the publish/subscribe pattern.
Commission Summary
    • A delegate encapsulates a behavior that contains a special return type and a set of parameters, similar to an interface that contains a single method.
    • The signature in the delegate type declaration determines which methods can be used to create the delegate instance, and also determines the signature of the call.
    • Creating a delegate instance requires a method and the target of the calling method (for instance methods).
    • The delegate instance is immutable (once created, it cannot be changed.) Don't worry about consistency and thread safety).
    • Each delegate instance contains a list of calls.
    • Delegate instances can be merged together, or they can be removed from one delegate instance China.
    • The event is not a delegate instance-it's just a pair of add/remove (a method/assignment method for similar attributes).


From for notes (Wiz)

One of the core foundations of C # 1--delegation

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.