c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2]
//Demo1:Declaring an event in an interface and implementing it in //a class. // event_keyword.cs
using System;
public delegate void MyDelegate(); // delegate declaration public interface I
{
event MyDelegate MyEvent;
void FireAway();
} public class MyClass: I
{
public event MyDelegate MyEvent; public void FireAway()
{
if (MyEvent != null)
MyEvent();
}
} public class MainClass
{
static private void f()
{
Console.WriteLine("This is called when the event fires.");
} static public void Main ()
{
I i = new MyClass(); i.MyEvent += new MyDelegate(f);
i.FireAway();
}
}
//Demo2:One use for accessor-declarations is to expose a large //number of events without allocating a field for each event, but //instead using a hash table to store the event instances. This is //only useful if you have a very large number of events, but you //expect most of the events will not be implemented. // event_keyword2.cs
using System;
using System.Collections; public delegate void MyDelegate1(int i);
public delegate void MyDelegate2(string s);
public delegate void MyDelegate3(int i, object o);
public delegate void MyDelegate4(); public class PropertyEventsSample
{
private Hashtable eventTable = new Hashtable(); public event MyDelegate1 Event1
{
add
{
eventTable["Event1"] = (MyDelegate1)eventTable["Event1"] + value;
}
remove
{
eventTable["Event1"] = (MyDelegate1)eventTable["Event1"] - value;
}
} public event MyDelegate1 Event2
{
add
{
eventTable["Event2"] = (MyDelegate1)eventTable["Event2"] + value;
}
remove
{
eventTable["Event2"] = (MyDelegate1)eventTable["Event2"] - value;
}
} public event MyDelegate2 Event3
{
add
{
eventTable["Event3"] = (MyDelegate2)eventTable["Event3"] + value;
}
remove
{
eventTable["Event3"] = (MyDelegate2)eventTable["Event3"] - value;
}
} public event MyDelegate3 Event4
{
add
{
eventTable["Event4"] = (MyDelegate3)eventTable["Event4"] + value;
}
remove
{
eventTable["Event4"] = (MyDelegate3)eventTable["Event4"] - value;
}
} public event MyDelegate3 Event5
{
add
{
eventTable["Event5"] = (MyDelegate3)eventTable["Event5"] + value;
}
remove
{
eventTable["Event5"] = (MyDelegate3)eventTable["Event5"] - value;
}
} public event MyDelegate4 Event6
{
add
{
eventTable["Event6"] = (MyDelegate4)eventTable["Event6"] + value;
}
remove
{
eventTable["Event6"] = (MyDelegate4)eventTable["Event6"] - value;
}
}
} public class MyClass
{
public static void Main()
{
}
}
//Deom3 public class SampleEventArgs
{
public SampleEventArgs(string s) { Text = s; }
public String Text {get; private set;} // readonly
}
public class Publisher
{
// Declare the delegate (if using non-generic pattern).
public delegate void SampleEventHandler(object sender, SampleEventArgs e); // Declare the event.
public event SampleEventHandler SampleEvent; // Wrap the event in a protected virtual method
// to enable derived classes to raise the event.
protected virtual void RaiseSampleEvent()
{
// Raise the event by using the () operator.
if (SampleEvent != null)
SampleEvent(this, new SampleEventArgs("Hello"));
}
}
参考链接:
http://msdn.microsoft.com/zh-cn/library/ms173171(v=vs.110).aspx
http://msdn.microsoft.com/zh-cn/library/awbftdfh.aspx
http://msdn.microsoft.com/zh-cn/library/8627sbea(v=vs.110).aspx
c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2]的更多相关文章
- Delegate(委托与事件)
Delegate可以当它是一个占位符,比如你在写代码的时候并不知道你将要处理的是什么.你只需要知道你将要引入的参数类型和输出类型是什么并定义它即可.这就是书本上所传达的方法签名必须相同的意思. 系统自 ...
- c# 关键字delegate、event(委托与事件)[MSDN原文摘录][1]
A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. ...
- Delegate event 委托事件---两个From窗体使用委托事件
窗体如下: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...
- 委托与事件--delegate&&event
委托 访问修饰符 delegate 返回值 委托名(参数); public delegate void NoReturnNoPara(); public void NoReturnNoParaMeth ...
- 浅谈C#委托和事件
委托给了C#操作函数的灵活性,我们可使用委托像操作变量一样来操作函数,其实这个功能并不是C#的首创,早在C++时代就有函数指针这一说法,而在我看来委托就是C#的函数指针,首先先简要的介绍一下委托的基本 ...
- 浅谈C#委托和事件【转】
委托给了C#操作函数的灵活性,我们可使用委托像操作变量一样来操作函数,其实这个功能并不是C#的首创,早在C++时代就有函数指针这一说法,而在我看来委托就是C#的函数指针,首先先简要的介绍一下委托的基本 ...
- 浅谈C#委托和事件(转载)
委托给了C#操作函数的灵活性,我们可使用委托像操作变量一样来操作函数,其实这个功能并不是C#的首创,早在C++时代就有函数指针这一说法,而在我看来委托就是C#的函数指针,首先先简要的介绍一下委托的基本 ...
- C#基础加强(8)之委托和事件
委托 简介 委托是一种可以声明出指向方法的变量的数据类型. 声明委托的方式 格式: delegate <返回值类型> 委托类型名(参数) ,例如: delegate void MyDel( ...
- C#语法之委托和事件
从大学就开始做C#这块,也做C#几年了,最近又从ios转回.Net,继续做C#,之前也没有写博客的习惯,写博客也是从我做ios的时候开始的,现在既然又做回了.net,那就写点关于.Net的博客,可能在 ...
随机推荐
- PhpStorm的注册码、Key
下面是PhpStorm的注册码.Key,其license由用户名和License值组成. User name: EMBRACE License key: ===== LICENSE BEGIN === ...
- Redis常用命令入门——列表类型(一级二级缓存技术)
获取列表片段 redis > LRANGE KEY_NAME START END lrange命令比较常用,返回从start到stop的所有元素的列表,start和stop都是从0开始. (1) ...
- 对象导论 Thinking in Java 第一章
1.1 抽象过程 1.人们能够解决问题的复杂性直接取决于抽象的类型和质量. 1.2 每个对象都有一个接口 1.3 每个对象都提供服务 1.4 被隐藏的具体实现 1.程序猿分为:类创建者 和 客户端程序 ...
- facebook 开源文件传输 lib
https://github.com/facebook/wdt WDT is an embeddedable library (and command line tool) aiming to tra ...
- php使用cURL实现Get和Post请求的方法
1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...
- poj1375Intervals(点到圆的切线)
链接 貌似这样的叫解析几何 重点如何求得过光源到圆的切线与地板的交点x坐标,可以通过角度及距离来算,如图, 根据距离和半径可以求得角度a.b.r,自然也可以求得d1,d2. 至于方向问题,在求r得时候 ...
- oracle的正则表达式
阅读目录 1.oracle(regular expression)简单介绍 2.oracle正则特殊字符 3.oracle正则字符簇 4.各种操作符的运算优先级 5.模拟测试例子 6.oracle对应 ...
- 使用SurfaceView
一.新建一个工程“LearnSurfaceView” 二.新建一个类“MySurfaceView” public class MySurfaceView extends SurfaceView imp ...
- ajax请求成功或失败的参数
success:function(response, status, xhr){ }, error:function(xhr, errorText, errorType){ alert(errorTe ...
- iOS开发之.pch文件初识
pch全称是“precompiled header”,即预编译头文件,自Xcode6诞生之日起,便在Supporting Files文件下消失多年.说起苹果对pch的爱恨情仇,其分析pch的作用便不言 ...