委托
委托(delegate)是C#中的一种类型,它实际上十一个能够持有对某个方法的引用的类。它本身能够拥有一个签名(signature),当它持有与它签名相同的方法的引用后,委托就可以和这个方法一样被使用。通过执行委托,就可以执行它所引用的方法,并且传递方法的参数等。它也可以作为参数完美的实现回调。
static void Main(string[] args)
{
MessagePrintDelegate msgd = new MessagePrintDelegate(PrintMessage);
msgd("Hello World");
![](/Images/OutliningIndicators/InBlock.gif)
}
static public void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(), msg);
}
匿名方法
我们不必为每一个委托实例都编写一个单独的方法,这时我们可以使用C#2.0中的匿名方法。
MessagePrintDelegate msgd = delegate(string msg)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(), msg);
};
msgd("Hello World");
多播委托
delegate void MessagePrintDelegate(string msg);
static void Main(string[] args)
{
MessagePrintDelegate msgd1,msgd2,msgd3;
msgd1 = PrintMessage;
msgd2 = PrintSomeThing;
msgd3 = msgd1 + msgd2;
msgd3("Visitor");
msgd3 -= msgd1;
msgd3("Only you!");
}
static public void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(), msg);
}
static public void PrintSomeThing(string msg)
{
Console.WriteLine("Your name is {0}",msg);
}
事件
事件(event)是一种允许类发送信号的方式,用来表明某个重要的事情要发生。事件可以通过委托施用event关键字来创建。为此首先要声明一个委托
static void Main(string[] args)
{
Task t = new Task();
t.OnNotifyProgress += new Task.ProgressChangedDelegate(OnNotify);
t.PerformTask();
![](/Images/OutliningIndicators/InBlock.gif)
}
static void OnNotify(int i)
{
Console.WriteLine("Progress Completed.{0}%Complete.", i);
}
![](/Images/OutliningIndicators/None.gif)
}
class Task
{
public delegate void ProgressChangedDelegate(int i);
public event ProgressChangedDelegate OnNotifyProgress;
public void PerformTask()
{
for (int i = 1; i <= 10000; i++)
{
if (i % 100 == 0)
{
OnNotifyProgress((int)(i / 100));
}
}
}
}
委托(delegate)是C#中的一种类型,它实际上十一个能够持有对某个方法的引用的类。它本身能够拥有一个签名(signature),当它持有与它签名相同的方法的引用后,委托就可以和这个方法一样被使用。通过执行委托,就可以执行它所引用的方法,并且传递方法的参数等。它也可以作为参数完美的实现回调。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
我们不必为每一个委托实例都编写一个单独的方法,这时我们可以使用C#2.0中的匿名方法。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
多播委托
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
事件
事件(event)是一种允许类发送信号的方式,用来表明某个重要的事情要发生。事件可以通过委托施用event关键字来创建。为此首先要声明一个委托
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)