Commission Summary
Summarize
As with Outputtime (), the compiler will put Outputtime (); compile into Outputtime.invoke ();
1 Defining delegate data types
2 Defining delegate variables
3 Determining whether a delegate type exists, using a delegate
if (outputtime! = null)
// {
As with Outputtime (), the compiler will put Outputtime (); compile into Outputtime.invoke ();
Outputtime.invoke ();
// }
File Write operation File.writealltext (@ "Aaa.text", System.DateTime.Now.ToString ("Yy-mm-dd hh-mm-ss"));
debugger, set breakpoints at the location you want to view, F10 next
General program to display time in two ways (console and write file)
Main program:classProgram {Static voidMain (string[] args) {Class1 T1=NewClass1 (); T1.outputtime=M1; T1.output1_time (); Class2 T2=NewClass2 (); T2.outputtime=M2; T2.output1_time (); Console.readkey (); } Static voidM1 () {Console.WriteLine ("Current time: {0}", System.DateTime.Now.ToString ()); } Static voidM2 () {File.writealltext (@"Aaa.text", System.DateTime.Now.ToString ("YY-MM-DD Hh-mm-ss")); }} class//define a delegate type Public Delegate voidDele_time (); Public classClass1 { PublicDele_time Outputtime; Public voidOutput1_time () {Console.WriteLine ("--------------------------------------------------"); Console.WriteLine ("--------------------------------------------------"); if(outputtime!=NULL) {outputtime (); } Console.WriteLine ("--------------------------------------------------"); Console.WriteLine ("--------------------------------------------------"); } } Public classClass2 { PublicDele_time Outputtime; Public voidOutput1_time () {Console.WriteLine ("--------------------------------------------------"); Console.WriteLine ("--------------------------------------------------"); if(Outputtime! =NULL) { //as with Outputtime (), the compiler will put Outputtime (); compile into Outputtime.invoke ( );Outputtime.invoke (); } Console.WriteLine ("--------------------------------------------------"); Console.WriteLine ("--------------------------------------------------"); } }
Pass-through code between forms
1Defining delegate Types Public Delegate voidDele_chuanzhi (stringstr); Form1 codePrivate voidButton1_Click (Objectsender, EventArgs e) {Form2 F2=NewForm2 (); F2. Show (); F2.chuanzhi=updatebtn; } Private voidUPDATEBTN (stringstr) {TextBox1.Text=str; }form2 CodePrivate voidTextbox1_keyup (Objectsender, KeyEventArgs e) { //This . Text = TextBox1.Text; stringUinput = This. TextBox1.Text; if(Chuanzhi! =NULL) {Chuanzhi (uinput); } }
Delegate--The value of 150525 between forms