Review the basics of .NET: DELEGATE, EVENT (event), implementing events from head to tail!

xiaoxiao2021-03-06  149

The implementation of DELEGATE and EVENT is unsatisfactory! It belongs to the basics!

There is such a .NET / C # test:

Please implement the concept of an event: The console screen is reciprocally in any string, and you type "You type:" You just type the string, if you type "q", exit the program, run!

The realization of the .NET's delegate and Event is unsuakable! It belongs to the basics!

Write an event response program under Windows, really uncommon from head to tail! I have two old articles:

"C # written by the httprequsetresponse class, asynchronous, event ... is also hot!" Http://www.9cbs.net/develop/read_article.asp?id=19254http://blog.9cbs.net/playueer/archive/ 2003/07/03 / 2856.aspx

"TreeView derived class: TreeViewEx Implement NodeshowTooltip, NodeDoubleClick event" http://www.9cbs.net/develop/read_article.asp?id=19246 http://blog.9cbs.net/playyuer/archive/2003/06/26 /2857.aspx

It is only the event of the implementation under WinForm!

My simplest reference answer is implemented in static methods: * // * Class class1 {public delegate void fireeventhandler (String s); public static event fireeventhandler firestatic; static bool b = false; static void main (String [ ] args) {firestatic = new fireEventhandler (Fire1); System.Console.writeline ("Please type any character (string), /" q / "exit!"); string s; while (true) {s = system. Console.readLine (); FireStatic (s); if (b == true) Break;}} static void fire1 (string s) {if (s! = "Q") {system.console.writeline ("You type : " s);} else {system.console.writeline (" Do not send it! "); b = true;}}} * // *

People who often write WinForm programs should be familiar. I think there should be some people who don't know how to implement the event! These people don't really know: this.button1.click = new system.eventhandler (this.button1_click) EventHandLer in the end How did it achieve? But this does not hinder the reply code of the Click event!

Today I also reviewed it, share with you with the C # Console program: Please pay attention to the program comment, pay attention to the screen prompt when running:

* /// If you are invited to any * .cs file, CSC is an exe file! Class class1 {// declares a delegation Delegate FireEventrandler, DELEGATE does not have a static or instance member, PUBLIC DELEGATE VOID FireEventHandler (String s);

// declare FireEventHandler // delegate type of event that is understood to be FireEventHandler data type, easy to understand public event FireEventHandler FireInstance; public static event FireEventHandler FireStatic; static void Main (string [] args) {System.Console.WriteLine ( "Hello World ");

// Use Delegate: FireEventHandler () to establish a delegate relationship // static method FireStatic () agent (entrusted) static method Fire1 () firestatic = new fireEventHandler (Fire1); // static method fiRestatic () agent () Entrusted) Instance method. Fire2 () firestatic = firestatic new fireeventhandler (new class1 (). Fire2;

Class1 c = new class1 (); // instance method .fireInstance () agent (entrusted) Instance method .fire2 () c.fireInstance = new fireEventhandler (C.fire2);

// instance method .fireInstance () Agent (entrusted) static method Fire1 () C.fireInstance = new fireEventhandler (Fire1) C.fireInstance; // The above is just a delegate relationship, has not implemented a delegate agent!

System.console.writeline ("The Print Relationship between:"); System.Console.writeline ("static method fiRestatic () is accepted as a proxy:"); ListdeLeGateRerance (FireStatic);

System.console.writeLine ();

System.Console.writeline ("instance method .fireInstance () is accepted as a proxy:"); ListDelegateRele (C.FireInstance);

System.console.writeline ("Please type any character (string), /" q / "exit!"); String s; while {s = system.console.readline (); system.console.writeline (" ------------------------ "); system.console.writeline (" The following is to Type / "{0} /" trigger events Response: "); // We have programmed this, namely, after accepting the screen line, the procedure executes the delegate agent method! //, triggered the event! // FireStatic static event triggered FireStatic (" FiRestic Static Event Trigrated: / nStatic method fiRestatic () The delegate agent executes {0} / n you type: [" S "] / N ");

// .fireInstance instance event triggered C.FireInstance ("FireInstance instance event triggered: / ninstance method. FireInstance () The delegate agent executed {0} / n You type: [" s "] / n "); system.console.writeline (" ======================================================= = "); System.console.writeline (" Please type any character (string), / "q /" exit! "); If (s ==" q ") Break;} /// * // Relatable relationship Use "- =" // to release the delegate, the remaining entrustment relationship ListdeGateRelation (FireStatic); FireStatic - = New FireEventHandler (Fire1); ListDelegateRele (FireStatic);

FireStatic = FireStatic - New FireEventHandler (New class1 (). Fire2; ListDelegateRerance (FireStatic);

C.FireInstance = New FireEventHandler (Fire1) - C.FireInstance; ListdeLeGateRelation (C.FireInstance);

C.fireInstance = C.fireInstance - New FireEventHandler (Fire1); ListDelegateRelan (C.FireInstance);

// * /}

// Static method static void fire1 (string s) {// Response Event program system.console.writeline (S.Replace ("{0}", "static method fire1 ()"));}

// Instance method Void Fire2 (String s) {// Response Event program system.console.writeline (S.Replace ("{0}", "instance .fire2 ()"));}

static void ListDelegateRelation (FireEventHandler x) {if (x! = null) {// foreach (System.Delegate d in x.GetInvocationList ()) foreach (FireEventHandler d in x.GetInvocationList ()) {System.Console.WriteLine (x .Tostring () "Events of the Type" (D.Method.Istatic? X.target "type of Instance method": D. Target "; D.Method.Name); }} Else {system.console.writeLine ("There is no delegation");}}}

/ * The following is a more complex point console program that triggers different events according to the packet of input numbers! * // * using system.text.RegularExpressions; class class1 {private static class1 x; static void main (string [] args ) {System.console.writeline ("Hello World"); x = new class1 (); // Set the delegate relationship // put the delegated to the left of the right side (- = is canceled) // The call to the "left" method signature is actually equivalent to the execution // of the "right side" method to execute // delegate the example method of the class1 to the instance event (X.fireInstance) X.fireInstance) X. FireInstance = new class1.fireEventhandler (x.fire1);

// Delegate Class1's static method (class1.fire2) to class1 instance event (X.fireInstance) x.fireInstance = new class1.fireeventhandler (class1.fire2);

// Delegate Class1 static method (class1.fire2) to class1.firestatic class1.firestatic = new class1.fireeventhandler (class1.fire3);

// Enter the instance method of class1 (x.fire1) to the Class1 Static Event (Class1.FireStatic) class1.firestatic = new class1.fireeventhandler (x.fire4);

INT i; string s; while {IF ((s = system.console.readline ()) == "q") {breaf;} if (regex.ismatch (s, @ "^ / d $") ) {I = system.convert.toint32 (s); if (i% 2 == 0) {// input, even, even trigger the event X.onfireInstance (); // Call instance method trigger event} else {// input When the base is triggered, the event class1.onfirestatic (); // calls the static method trigger event}}}} public string fire1 () // instance method {system.console.writeline ("Input is even"); return null;} public Static string fire2 () // Static method {system.console.writeline ("Input is even"); return null;} public static string fire3 () // Static method {system.console.writeLine ("Input is odd") Return null;} public string fire4 () // instance method {system.console.writeline ("Input is an odd number"); Return NULL;}

// Most delegates do not need to return values!

Public Delegate String FireEventHandler (); // Instance Event Public Event FireEventHandler FireInstance; // Static Event Public Static Event FireEventHandler FireStatic;

// Instance method PUBLIC VOID ONFIREINSTANCE () {// instance method triggers instance event // According to the declaration of the principal: // Entrust execution: Execute Fire1 and Fire2 this.fireInstance (); // instance method trigger instance Event entrustment execution system.console.writeline ("The following is an error answer: just the test /" instance method / "trigger /" static event / "); // instance method triggers static event / / According to the principal, the declaration: / / Entrust execution: Execute the Fire3 and Fire4 FireStatic (); // instance method trigger static event entrustment execution}

// Static method public static void onfirestatic () {// Static method triggers static event // According to the declaration of the principal: // Delegate execution: Implement Fire3 and Fire4 FireStatic ();

System.console.Writeline ("The following is an error answer: just the test /" static method / "trigger /" instance event / "); // instance member X is called instance event method here! // Static method triggers instance Event // According to the declaration of the principal: // Delegate execution: Implement the Fire1 and Fire2 X.fireInstance (); // Static method trigger instance event entrustment execution}} * / answer below the netizen problem class apptest {static void Main (String [] args) {witheventsclass.firestatic = new witheventsclass.fireEventhandler (Fire1); system.console.writeline ("Please type any character (string), /" q / "exit!"); WitHeventsclass.Run );} Static void fire1 (string s) {if (s! = "Q") {system.console.writeline ("You type:" s);} else {system.console.writeline ("Do not send it ! "); Witheventsclass.b = true; witheventsclass.firestatic - = new witheventsclass.fireEventhandler (fire1) ;;}}}

class WithEventsClass {public delegate void FireEventHandler (string s); public static event FireEventHandler FireStatic; public static bool b = false; public static void Run () {string s; while (! b) {s = System.Console.ReadLine () ; If (FireStatic! = Null) {firestatic (s);}}}} // =============================== ========= // instance1class AppTest {public WithEventsClass _wec; static void Main (string [] args) {AppTest a = new AppTest (); WithEventsClass wec = new WithEventsClass (); wec.FireInstance = new WitHeventSclass.fireEventHandler (a.wec_fireinstance); a._wec = wec; system.console.writeline ("Please type any character (string), /" q / "exit!"); Wec.Run ();} private void Wec_FireInstance (string s) {if (s! = "q") {system.console.writeline ("You type:" s);} else {system.console.writeline ("Don't send it!"); _wec. B = true; _wec.fireInstance - = new witHeventsclass.fireEventhandler (this.wec_fireinstance);}}}

class WithEventsClass {public delegate void FireEventHandler (string s); public event FireEventHandler FireInstance; public bool b = false; public void Run () {string s; while (this.b!) {s = System.Console.ReadLine (); IF (this.fireInstance! = null) {this.fireinstance (s);}}}} // =========================== ====================== // instance2class appteest {static void main (string [] args) {apptest a = new apptest (); witheventsclass wec = new WitHeventsClassClass (); Wec.fireInstance = new witheventsclass.fireEventhandler (a.wec_fireinstance); system.console.writeline ("Please type any character (string), /" q / "exit!"); Wec.Run ();} Private Void Wec_FireInstance (WitHeventSclass Sender) {if (Sender.s! = "Q") {system.console.writeline ("You type:" sender.s);} else {system.console.writeLine ("Do not send ! a "); Sender.b = true; Sender.FireInstance - = new WithEventsClass.FireEventHandler (this.wec_FireInstance);}}} class WithEventsClass {public delegate void FireEventHandler (WithEventsClass Sender); Public Event FireEventHandler FireInstance; Public Bool B = False; Public String S; Public Void Run () {While (! this.b) {this.s = system.console.readline (); if (this.fireinstance! = null) {This.fireinstance (this);}}}}

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

New Post(0)