C#: Delegate and Event
2014-03-21 04:49
309 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test.Other { class Program { //public static delegate void TestDelegate(); //public static event TestDelegate TestEventHandler; static void Main(string[] args) { // Delegate can be triggered directly //TestDelegate testDelegate = new TestDelegate(Test); //testDelegate(); //TestDelegateAndEvent test = new TestDelegateAndEvent(); //test.TestEventHandler += Test; // Error: The delegate inner the class is private even if the type is public. // test.TestDelegate(); // Error: Event can't be triggered directly. //test.TestEventHandler(); Console.ReadLine(); } public static void Test() { Console.WriteLine("test"); } } }
Program.cs
总结:
1. Delegate可以将方法作为另外一个方法的参数带入其中进行运算;
2. 实际上Delegate和Event都是类,Event是一种特殊类型的Delegate;
3. 类内的Delegate不能被对象调用,即便这个delegate是Public的;
4. delegate和event都重载了+=,-=, 但是delegate可以用=直接赋值,但是event不可以;
5. delegate和event都不能由对象直接触发;
6. delegate和event都不能是static.
相关文章推荐
- c# TAB键定位的顺序
- C# 在窗口中,敲回车进行登录。(效果是在登录界面一敲回车就点击登录按钮)
- C#图解教程读书笔记(第1章 C#和.net框架)
- C# textbox输入的时候显示是****
- C# 窗口居中显示
- C#图解教程读书笔记(第15章 委托)
- Essential C#读书笔记
- c#实现数据集合转换为csv文本
- C#调试入门篇
- C#动态加载树菜单
- C# 继承
- 【C#】结对项目开发-电梯调度仿真系统(Bata版)(党云龙、黄为)
- C# 读取excel 文件的方法
- C#给线程传递参数有3种方式
- C# 类与结构的区别
- c# 学习笔记
- C# WaitHandle 使用教程
- System.Diagnostics.Process.Start的用法(C#)
- C#-设置label的字体颜色和大小
- C# WinForm程序退出的方法