C # sharp experience the ninth lecture and the event

xiaoxiao2021-03-06  68

The ninth lecture and incident

Delegation delegate is a new type of data type introduced in C #, which is very similar to function pointers in C / C , which often uses dynamic methods called when compiling. However, it is different from the function pointer to delegate the object-oriented object, which can reference the static method or the instance method, and the function pointer can only reference the static method. The delegation in C # is also a type of security. As an object-oriented data type, the delegated use is divided into three steps: delegation declaration, delegate instantiation and delegation calls. Delegation declares that the data type of the method (static method or instance method) that encapsulates a specific parameter type and the return value type, see the following example: Delegate Int compute (int LEFT, INT RIGHT); can be seen, delegated type SAY The two elements-parameter types and return value types are included. The delegation type and method only satisfies the following two conditions, we say they are compatible: 1. The number of parameters is the same, and their type is also the same as 2. The return value is the same delegate type, which is the same type (the same name). The equality of delegated instances refers to the method chain of their binding method to the same method or the same approach, and their own type can be compatible (equally satisfied with two conditions), do not have to force The same name. Delegation instantiation is about to delegate type binding specific methods, and other objects of other objects, you need to use new statements, just accept and the method name compatible with the delegate as a parameter of the new statement. If it is an instance method, an instance object and method must be provided simultaneously in the middle of the intermediate plus point number. After delegate instantiation, you can delegate calls like a modified method. Here is a complete example, more typically three steps we usually use: use system; delegate int compute (int ost, int right); // delegate type declaration class a {public int mul (int Num1, INT NUM2) {RETURN NUM1 * NUM2;}} Class Test {Public Static Int Add (Int Num1, Int Num2) {RETURN NUM1 NUM2;} static void main () {compute c1 = new compute (add); // Static Method of instantiation A a = new a (); compute c2 = new compute (amul); // instance method of instance method INT R1 = C1 (2, 3); // Delegation call INT R2 = C2 (2 , 3); // Delegate call console.writeline (R1); Console.Writeline (R2);} The delegation combination delegation is referring to a delegate type to bind multiple calls simultaneously. Due to the multiple methods of binding, C # specifies the return type of the combination delegate must be Void. Of course, the parameter type of these methods must also be compatible with the parameter type of the combination. The delegation combination uses " " or " =" to combine two delegated types into a new combination delegation type. "-" or "- =" is used to remove a delegate instance that binds a method or a combination of a combination delegation that binds multiple methods from the combined delegation type. It should be noted that the delegation type of the participating operation must be the same when making a delegate combination and removal operation - Note that "the same" is not "equal".

Look out below: use system; delegate void mydelegate (string s); class test {public static void hello (string s) {console.writeline ("Hello, {0}!", S);} public static void goodbye String s) {console.writeline ("Goodbye, {0}!", s);} public static voidmain

() {MyDelegate A, B, C, D; a = new mydelegate (hello); b = new mydelegate (goodbye); c = a b; // delegated combination D = C - a; // delegated shift Except A ("a"); b ("b"); c ("c"); d ("d");}}}} program output: Hello, A! Goodbye, B! Hello, C! Goodbye, C! Goodbye, D! We can see that after the delegate combination, the combination delegation type C will bind two methods Hello (String S) and Goodbye (String S). From its output we can see this. It should be noted that the method call in the combination delegation is in order, if we modify C = A B in the example; C = B A; we will see the change in the output sequence. The same delegation is also in order, it always starts searching for the delegation instance from the final start of the delegate instance - Note that the removal here is in units of delegate, not by way of. The reader can change the above example to see the results: public static void

Main

() {MyDelegate A, B, C, D; a = new mydelegate (hello); b = new mydelegate (goodbye); c = a b a; // delegated combination B = B; // delegated combination D = C - b; // Delegation removal A ("a"); b ("b"); c ("c"); D ("d");} Event event is a Class C # Members, it has the same modifier and corresponding semantics. Simply put, an event is a message notification that is a way to pass messages between objects. C # uses a logic called "Publish-Registration-Accept" to deliver messages between objects, informing an event, such as the mouse to the click, email arrival of a button, and more. An object only has power to notify other objects when it comes to the event, and an object is only qualified to accept an object after the event occurs after the event occurs. Note that the event that happens here is in the event publisher object, and this event is necessary to let another object know, this has the basis of such an event model. The basis and necessary. In the event model, the event sender did not know which object to accept such an event. But also to notify it, what should I do? This requires us to use our debut. Below is a delegation example of the event: Public Delegate Void SomeeventHandler (Object Sender, Someeventargs E); I have a delegation to do an event declaration: public evenetaeventhandler someevent; // event declare an object to accept an event must be in it The method of processing the corresponding event is provided in the class declaration: public class somereceiverclass {. . . Other implementations of the class (Object sender, someeventargs e) {. . . // How to deal with the event. }} And an event sender's object must provide event declarations in its class declaration, and the corresponding event trigger mechanism: public class somesenderclass {. . . Other implementations of the class public event someeventrandler someevent; // event declared Protected Virtual Void Onsomeevent (Someevent! = Null) someevent (this, e);} public void sometigueermethod () {. . . // Other implementations.

Someeventargs E = New SomeeventArgs (...); // Instantian Event Parameters OnsomeEvent (e); // Trigger the event. }} The connection between the present incident, the method, trigger method, the sender and the recipient of the service - the delegation is built, only the work we registered during the last programming: public class someapp {public static staticMain

(String [] args) {SomeEventReceiver myReceiver = new SomeEventReceiver (); // instantiate the event receiver SomeEventReceiver mySender = new mySender (); // instantiate event sender MySender.SomeEvent = new SomeEventHandler (myReceiver.SomeEventProcessMethod); / / Register event. . . // Other Operations that may trigger the event of somelent. }} Note The registration operation is registered or unregistered with the symbol of " =" and "- =". To achieve multicast event, we can register multiple times: SomeEventReceiver mySender = new mySender (); // instantiate the event sender SomeEventReceiver1 myReceiver1 = new SomeEventReceiver1 (); // instantiate the event receiver SomeEventReceiver2 myReceiver2 = new SomeEventReceiver2 ( ); // instantiate the event receiver mySender.NewMailEvent = new NewMailEventHandler (myReceiver1 SomeEventProcessMethod);. mySender.NewMailEvent = newNewMailEventHandler (myReceiver2 SomeEventProcessMethod);. finally, we give a complete e-mail notification procedures, detailed notes and refueling, we The C # event model and its underlying mechanism can be studied more deeply on this basis.

using System; // Event message channel parameter class public class NewEmailEventArgs: EventArgs {public NewEmailEventArgs (string subject, string message) {this.subject = subject; this.message = message;} public string Subject {get {return (subject); }} public string message {get {return (message);}} string subject; string message;} // declare a new message delegate public delegate void NewMailEventHandler (object sender, NewEmailEventArgs e); // send message class declaration public class EmailSender { // declare a new event message public event NewMailEventHandler NewMailEvent; protected void OnNewMail (NewEmailEventArgs e) {if (NewMailEvent = null!) NewMailEvent (this, e);} // send Mail public void SendMail (string subject, string message) {NewEmailEventArgs E = new newemaileventargs (Subject, Message); OnneWMail (e);}} // mail recipient declared public class emailreceiver {public emailReceiver (String Name) ) {_Name = Name;} // Processing the method of receiving a new mail event public void comMemail (Object sender, newemaileventargs e) {Console.Writeline ("this is receiver {0}, receive a new email: / n {1} {2} ", _ name, e.subject, e.Message);} String _name;} // Test class public class test {public static voidmain

转载请注明原文地址:https://www.9cbs.com/read-119999.html

New Post(0)