Delegate, event

Source: Internet
Author: User

Delegate: Delegate is a table? The method is passed as a parameter to other methods. A delegate is similar to a function pointer, but unlike a function pointer, a delegate is a. To an object
, type-safe and insured. A delegate can lead to a static or an instance of a law.

The delegate is a class, it defines the type of the law, making it possible to speak the law as another? method, which is dynamically assigned to the parameter
Number of practices that can be avoided in the program? If-else (Switch) statement, while making the program more extensible.

The object of the delegate has an attribute: can make? The + operator assigns multiple objects to a delegate instance. The multicast delegate contains the assigned delegate's
List. When you tune a multicast delegate, it is ordered in order.
Sorrento Note: Only delegates of the same type can be merged.

When you make an event design pattern.
When the encapsulation static method is desirable.
When the tune?? You do not need to access other properties,? methods, or connect in the object that implements the method.
The combination of the need?
When a class may require multiple implementations of the method.

The delegate's declaration prototype is as follows:
Delegate < function return type > < delegate name > (< function parameter >)
The delegation is divided into three steps as follows:
1. Delegation of Declaration
2. Delegate instantiation

3. Invocation of a delegate
      

classProgram { Public Delegate voidGreeting (stringname);  Public Static voidEnglishgreeting (stringname) {Console.WriteLine ("Good morning!~"+name); }         Public Static voidChinesegreeting (stringname) {Console.WriteLine ("Good morning,!~ ."+name); }        Static voidMain (string[] args)            {greeting greet; Greet=chinesegreeting; Greet+=englishgreeting; Greet ("Zhang San"); Greeting GRE=Newgreeting (chinesegreeting); GRE+=englishgreeting; GRE ("Zhang San"); }    }

Event: A class or object can notify other classes or objects about the issue through events. The class that sends (or raises) an event is called a "sender", and the class that receives (or processes) The event is called a "subscriber."
Note: The essence of C # events is the encapsulation of messages.

Origin: The delegate, in a way, destroys the encapsulation of the object. The "=" is the assignment statement because it is to be entered? instance
When the first law was registered, it was "+ =". However, whether it is assignment or registration, it is bound to the delegate, in addition to the tune?
When the order is different, there is no difference. This makes it very awkward, so the event, the appearance!

Is the event encapsulates a variable of a delegate type, equivalent to the amount of a delegate type? property.
Pros: Inside a class, whether it's public or protected, he's always private.
Outside of the class, the access qualifier that registers "+ =" and unregisters "-=" is the same as the access qualifier that is made when the event is declared.

classProgram { Public Delegate voidGreeting (stringname);  Public classGreetingmanger { Public Eventgreeting Makegreet;  Public voidGreetpeople (stringname)            {Makegreet (name); }        }         Public Static voidEnglishgreet (stringname) {Console.WriteLine ("Good morning!~"+name); }         Public Static voidChinesegreet (stringname) {Console.WriteLine ("Good morning! "+name); }        Static voidMain (string[] args) {Greetingmanger GRE=NewGreetingmanger (); Gre. Makegreet+=Englishgreet; Gre. Makegreet+=Chinesegreet; Gre. Greetpeople ("Zhang San"); }    }

The C # phase is basically over, and then there's hell u-3d.

A little excited.

Delegate, event

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.