Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于.Net中的委托_C# - Fatal编程技术网

C# 用于.Net中的委托

C# 用于.Net中的委托,c#,C#,可能重复: Net中委托的实际用途是什么?C#中的委托允许您调用在运行时指定的一组方法,而不是在编译时指定的一个方法。 系统中的事件是使用委托来处理的,因为委托允许它们引发一个事件并调用由订阅该事件的不同侦听器指定的一系列方法 public class EventRaiser { public delegate void SomethingHappened(); public SomethingHappend OnSomethingHappened { get; se

可能重复:

Net中委托的实际用途是什么?C#中的委托允许您调用在运行时指定的一组方法,而不是在编译时指定的一个方法。 系统中的事件是使用委托来处理的,因为委托允许它们引发一个事件并调用由订阅该事件的不同侦听器指定的一系列方法

public class EventRaiser
{
    public delegate void SomethingHappened();

    public SomethingHappend OnSomethingHappened
    { get; set; }
}

public class Listener
{
    public void DoSomething()
    {
        //Do something
    }
}

public class OtherListener
{
    public void DoSomethingDifferent()
    {
        //Do something different
    }
}

EventRaiser raiser = new EventRaiser();
Listener listener = new Listener();
OtherListener other = new OtherListener();

raiser.OnSomethingHappened += listener.DoSomething;
raiser.OnSomethingHappened += other.DoSomethingDifferent;

//This call will call both DoSomething and DoSomethingDifferent
raiser.OnSomethingHappened();

Aman,我建议你浏览一下.net上的一些在线资料。这是一个非常基本的主题,即使在堆栈溢出中也有足够的答案(-1)你可以很容易地“用谷歌搜索”这个!