委托用于方法的引用,无方法体,只有参数列表和返回值相同时才可使用委托
using System;
namespace DelegateAppl
{
class PrintString
{
// 委托声明
public delegate void PrintStrings(string s);
// 该方法打印到控制台
public static void WriteToScreen(string str)
{
Console.WriteLine("The String is: {0}", str);
}
// 该方法把委托作为参数,并使用它调用方法
public static void SendString(PrintStrings ps)
{
ps("Hello World");
}
static void Main(string[] args)
{
SendString(WriteToScreen);
}
}
}