This function is similar to action, but the parameter input function is added.
Use delegateCode:
Using System;
Using System. Windows. forms;
Public Delegate VoidDisplaymessage (StringMessage );
Public Class Testtestdelegate
{
Public Static Void Main ()
{
Displaymessage showmethod = displaytowindow;
Showmethod ( " Zhangps " );
}
Public Static Void Displaytowindow ( String Strname)
{
MessageBox. Show ( " Antiquity way: Hello world, " + Strname );
}
}
Action <of T> code:
Using System;
Using System. Windows. forms;
Public Class Testtestdelegate
{
Public Static Void Main ()
{
Action < String > Showmethod = displaytowindow;
Showmethod ( " Zhangps " );
}
Public Static Void Displaytowindow ( String Strname)
{
MessageBox. Show ( " Action way: Hello world, " + Strname );
}
}
More concise method:
Using System;
UsingSystem. Windows. forms;
Public class testtestdelegate
{< br> Public static void main ()
{< br> action string showmethod = s =>{ MessageBox. show ( " action way: Hello world, " + S) ;};
showmethod (" zhangps " );
}< BR >}
The method passed as the parameter (
Delegate, pointer similar to C ++):
Using System;
Using System. Windows. forms;
Public Delegate VoidDisplaymessage (StringMessage );
Public Class Testtestdelegate
{
Public Static Void Main ()
{
Invokedispaly (displaytowindow );
}
Public Static Void Invokedispaly (displaymessage delegateargs)
{
Delegateargs (" Zhangps " );
}
Public Static Void Displaytowindow ( String Strname)
{
MessageBox. Show ( " Delegate Param way: Hello world, " + Strname );
}
}
Reference: http://msdn.microsoft.com/zh-cn/library/018hxwa8.aspx