委托和事件 [英] delegates and events
问题描述
我创建了一个非常简单的虚拟程序来理解委托和事件。在我下面的程序,我简单的调用方法。当我打电话的方法,五种方法将被自动调用委托和事件的帮助。
请看看我的程序,做让我知道我错了。或右侧,因为这是一个使用委托和事件我第一次
使用系统;
命名ConsoleApplication1
{
公众委托无效MyFirstDelegate();
类测试
{
公共事件MyFirstDelegate myFirstDelegate;
公共无效调用()
{
Console.WriteLine(欢迎在世界上代表..);
如果(myFirstDelegate!= NULL)
{
myFirstDelegate();
}
}
}
类AttachedFunction
{
公共无效firstAttachMethod()
{
控制台。的WriteLine(ONE ...);
}
公共无效SecondAttachMethod()
{
Console.WriteLine(两个...);
}
公共无效thirdAttachMethod()
{
Console.WriteLine(三......);
}
公共无效fourthAttachMethod()
{
Console.WriteLine(四......);
}
公共无效fifthAttachMethod()
{
Console.WriteLine(五......);
}
}
类MyMain
{
公共静态无效的主要()
{
测试测试=新测试( );
AttachedFunction attachedFunction =新AttachedFunction();
test.myFirstDelegate + =新MyFirstDelegate(attachedFunction.firstAttachMethod);
test.myFirstDelegate + =新MyFirstDelegate(attachedFunction.SecondAttachMethod);
test.myFirstDelegate + =新MyFirstDelegate(attachedFunction.thirdAttachMethod);
test.myFirstDelegate + =新MyFirstDelegate(attachedFunction.fourthAttachMethod);
test.myFirstDelegate + =新MyFirstDelegate(attachedFunction.fifthAttachMethod);
test.Call();
到Console.ReadLine();
}
}
}
事件是使用委托实施。按照惯例事件表示采取以下形式:
无效事件处理(对象发件人,EventArgs参数);
的事件处理实际上是在.net中定义的代表。 EventArgs的是.NET中的一类,它作为一个占位符来传递更多的信息。如果你有更多的信息,你会创建一个从EventArgs的派生和包含的附加数据属性的类;因此,你可以创建自己的委托,像这样:
无效一个MyEventHandler(对象发件人,MyEventArgs参数);
I have created a very simple dummy program to understand Delegates and events. In my below program I am simple calling a method. When I call a method, five methods are automatically called with the help of delegates and events.
Kindly take a look at my program and do let me know where I am wrong or right as this is my first time using delegates and events.
using System;
namespace ConsoleApplication1
{
public delegate void MyFirstDelegate();
class Test
{
public event MyFirstDelegate myFirstDelegate;
public void Call()
{
Console.WriteLine("Welcome in Delegate world..");
if (myFirstDelegate != null)
{
myFirstDelegate();
}
}
}
class AttachedFunction
{
public void firstAttachMethod()
{
Console.WriteLine("ONE...");
}
public void SecondAttachMethod()
{
Console.WriteLine("TWO...");
}
public void thirdAttachMethod()
{
Console.WriteLine("THREE...");
}
public void fourthAttachMethod()
{
Console.WriteLine("FOUR...");
}
public void fifthAttachMethod()
{
Console.WriteLine("FIVE...");
}
}
class MyMain
{
public static void Main()
{
Test test = new Test();
AttachedFunction attachedFunction = new AttachedFunction();
test.myFirstDelegate += new MyFirstDelegate(attachedFunction.firstAttachMethod);
test.myFirstDelegate += new MyFirstDelegate(attachedFunction.SecondAttachMethod);
test.myFirstDelegate += new MyFirstDelegate(attachedFunction.thirdAttachMethod);
test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fourthAttachMethod);
test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fifthAttachMethod);
test.Call();
Console.ReadLine();
}
}
}
Events are implemented using Delegates. That said by convention events take the form of:
void EventHandler(Object sender, EventArgs args);
EventHandler is actually a delegate defined in .Net. EventArgs is a class in .Net that acts as a placeholder to pass additional information. If you have additional information you would create a class that derived from EventArgs and contained properties for the additional data; therefore you would create your own delegate like so:
void MyEventHandler(Object sender, MyEventArgs args);
Microsoft has a tutorial on events here and also describes defining and raising events here
这篇关于委托和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!