一种是常规的
delegate void MyDelegate();public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
}
{
static public void Main()
{
MyClass p = new MyClass(); // Map the delegate to the instance method:
MyDelegate d = new MyDelegate(p.InstanceMethod);
d();
}
}另一种我就觉得奇怪了,但是也符合委托的定义,就是方法调用方法的参数
using System.Runtime.InteropServices;public delegate bool WNDENUMPROC(IntPtr hwnd, int lParam);[DllImport("user32.dll")]
public static extern int EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);///public static bool EnumFunc(IntPtr hwnd, int lParam)
{
Console.WriteLine("Window handle is " + hwnd);
return true;
}private void button1_Click(object sender, EventArgs e)
{
EnumWindows(EnumFunc, 0);
}
上面加三斜杠的地方就是我不明白俩者的区别,为什么这个委托跟第1个不一样,这个是方法里有个委托类型的参数
delegate void MyDelegate();public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
}
{
static public void Main()
{
MyClass p = new MyClass(); // Map the delegate to the instance method:
MyDelegate d = new MyDelegate(p.InstanceMethod);
d();
}
}另一种我就觉得奇怪了,但是也符合委托的定义,就是方法调用方法的参数
using System.Runtime.InteropServices;public delegate bool WNDENUMPROC(IntPtr hwnd, int lParam);[DllImport("user32.dll")]
public static extern int EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);///public static bool EnumFunc(IntPtr hwnd, int lParam)
{
Console.WriteLine("Window handle is " + hwnd);
return true;
}private void button1_Click(object sender, EventArgs e)
{
EnumWindows(EnumFunc, 0);
}
上面加三斜杠的地方就是我不明白俩者的区别,为什么这个委托跟第1个不一样,这个是方法里有个委托类型的参数
5大类型你都不懂?
class
struct
delegate
interface
enum少见多怪!