using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program pg = new Program();
helloDelegate myHelloDelegate = new helloDelegate(pg.ReturnMessage);
string message = myHelloDelegate("Kangting");
Console.WriteLine(message);
Console.ReadKey();
}
public string ReturnMessage(string pName)
{
string message = "Hello,";
message += pName;
return message;
}
public delegate string helloDelegate(string pName);
}
}
我將之改寫成VB2008之程式碼(主控台應用程式專案):
Module Module1
Public Delegate Function helloDelegate(ByVal pName As String) As String
Public Function ReturnMessage(ByVal pName As String) As String
Dim message As String = "Hello,"
message &= pName
Return message
End Function
Sub Main()
Dim myHelloDelegate As New helloDelegate(AddressOf ReturnMessage)
Dim message As String = myHelloDelegate.Invoke("Kangting")
Console.WriteLine(message)
Console.ReadKey()
End Sub
End Module
再進一步將之改寫為:
Module Module1
Public Delegate Function helloDelegate(ByVal pName As String) As String
Public Function ReturnMessage(ByVal pName As String) As String
Dim message As String = "Hello,"
message &= pName
Return message
End Function
Sub Main()
'Dim myHelloDelegate As New helloDelegate(AddressOf ReturnMessage)
'Dim message As String = myHelloDelegate.Invoke("Kangting")
'Console.WriteLine(message)
Dim myHelloDelegate As helloDelegate = Function(pName As String) "Hello," & pName
Console.WriteLine(myHelloDelegate("Kangting"))
Console.ReadKey()
End Sub
End Module
沒有留言:
張貼留言