同标题!
解决方案 »
- 用linq怎么判断一个字符串是否在dataset里面存在?
- Console.WriteLine(t.plate);什么意思,怎么读下面的程序?
- 调查一下:说说上大学期间你做过最龌龊的事
- 比较两个文件数组中不一样的文件
- 我来对比一下C#和VB.NET 开发WINFORM程序 的优缺点,大家看看我说的对吗?
- 在弹出窗口中,怎么读取,图片路径
- mschart注册问题
- 请教 ZEBRA QL 220 Plus 带Wireless的开发
- 我有个ocx组件想用web方式发布,好像要先打包到.cab文件,该怎么做了?
- treeView中如何更改当前选择的节点?
- 关于visual studio2010打包问题
- 两个FileInfo[]数组如何合并成一个?
{
//
}
{
bar((s, e) => Console.WriteLine("Hello World"));
}void bar(EventHandler eh)
{
eh(this, new EventArgs());
}
小弟自已做了一个例子,当然是在别人的例子上做了一些修改,请老板们点评using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class Publisher
{
public event EventHandler OnPublish;
public void Issue(object sender, EventArgs e)
{
if (OnPublish != null)
{
Console.WriteLine("发行刊物");
OnPublish(sender, e);
}
}
}
class Subscriber
{
public void Receiver(object sender, EventArgs e)
{
Console.WriteLine("订阅者已收到了刊物");
}
}
class Program
{
static void Main()
{
Publisher pub = new Publisher();
Subscriber zs = new Subscriber();
pub.OnPublish += new EventHandler(zs.Receiver);
pub.Issue(pub, new EventArgs());
Console.Read();
}
}
}因为微软官方上CSND中没有相关EventHandle的实例,所以做此
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] Args)
{
foo();
}
static void foo()
{
bar((s, e) => Console.WriteLine("Hello World"));
}static void bar(EventHandler eh)
{
eh(this, new EventArgs());
}
}
}这个程序比你的那个简洁。而且我想说明的是,它就是一个普通的委托。不要看到 Event 就觉得必须是事件,必须订阅发布。
至于我的例子的事件执行,一定要按定义发行者,订阅者,订阅事件,触发事件,事件处理程序,其目的更好的理解事件。
x(null,null);这也就可以通过变量x来调用方法method1了。不过这显得很多余了,我们不知道此时为什么要使用EventHandler。
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class Publisher
{
public event EventHandler OnPublish;
public void Issue()//
{
if (OnPublish != null)
{
Console.WriteLine("发行刊物");
OnPublish(this, null);
}
}
}
class Subscriber
{
public void Receiver(object sender, EventArgs e)
{
Console.WriteLine("订阅者已收到了刊物");
}
}
class Program
{
static void Main()
{
Publisher pub = new Publisher();
Subscriber zs = new Subscriber();
pub.OnPublish += new EventHandler(zs.Receiver);
pub.Issue();
Console.Read();
}
}
}
if (del != null)
{
Console.WriteLine("发行刊物");
del(this, null);
}