C#: Delegate and Event
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test.Other
{
public class TestDelegateAndEvent
{
public delegate void TestDelegate();
public event TestDelegate TestEventHandler; public void Test()
{
TestDelegate testDelegate = new TestDelegate(Test1);
TestEventHandler += new TestDelegate(Test1); testDelegate();
TestEventHandler();
}
public void Test1()
{
}
}
}
TestDelegateAndEvent.cs
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#: Delegate and Event的更多相关文章
- 浅谈c#中的delegate和event了
一.开篇忏悔 对自己最拿手的编程语言C#,我想对你说声对不起,因为我到现在为止才明白c#中的delegate和event是怎么用的,惭愧那.好了,那就趁着阳光明媚的早晨简单来谈谈delegate和ev ...
- 第一章、C#委托和事件(Delegate、Event、EventHandler、EventArgs)
第一章.C#委托和事件(Delegate.Event.EventHandler.EventArgs) 分类: 学习笔记-C#网络编程2012-12-08 14:10 7417人阅读 评论(3) 收藏 ...
- delegate和event
经过查阅资料和自己的理解整理出来的,欢迎大家指教. delegate和event 何时使用: 异步的时候,比如加载完成通知. 需要回调的时候,比如按钮点击.动画播放结束等. 发送事件通知的时候. 比如 ...
- .NET 中易混淆的概念(Delegate vs Event)
事件(event)是一个非常重要的概念,我们的程序时刻都在触发和接收着各种事件:鼠标点击事件,键盘事件,以及处理操作系统的各种事件.所谓事件就是 由某个对象发出的消息.比如用户按下了某个按钮,某个文件 ...
- 观察者模式与.NET的delegate、event机制
1.引言 最近在写一些程序玩的时候,接触到了delegate(委托)和event(事件),网上查找了很多的资料,有些博文说可以把delegate近似当做C++当中的函数指针来看,由于自己本身对C++的 ...
- [转] C#中的delegate 和 event
转至:here 终于会用c#中的delegate(委托) 作者:qq826364410 引言 Delegate是Dotnet1.0的时候已经存在的特性了,但由于在实际工作中一直没有机会使用Delega ...
- delegate 和 event
delegate 和 event 观察者模式 这里面综合了几本书的资料. 需求 有这么个项目: 需求是这样的: 一个气象站, 有三个传感器(温度, 湿度, 气压), 有一个WeatherData对象, ...
- [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)
原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...
- C#中Delegate和Event以及它们的区别(转载)
一.Delegate委托可以理解为一个方法签名. 可以将方法作为另外一个方法的参数带入其中进行运算.在C#中我们有三种方式去创建委托,分别如下: public delegate void Print( ...
随机推荐
- 300道随机四则运算小程序(java编写)
这是由Java编写的300道随机四则运算小程序, 运算数范围为0~100(不包括100),运算结果保留两位小数. 程序代码: import java.util.*; class Yunsuan{ pu ...
- [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)
1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...
- Java Head First & 多态
package com.cwcec.tag; class Fruit { } class Apple extends Fruit{} class Animal { public Fruit eat(F ...
- RPM 安装oracle18c 修改字符集的方法
1. 安装完preinstall 和 oracle 的rpm版本之后 到这个界面 rpm -ivh oracle-database-preinstall-18c-.el7.x86_64.rpm war ...
- .net MVC4 来一个简单的分页代码
action VodeEntities db = new NewVode.Models.VodeEntities(); ; ) { var sql = "select * ,row_numb ...
- 【uoj#209】[UER #6]票数统计 组合数+乱搞
题目描述 一个长度为 $n$ 的序列,每个位置为 $0$ 或 $1$ 两种.现在给出 $m$ 个限制条件,第 $i$ 个限制条件给出 $x_i$ .$y_i$ ,要求至少满足以下两个条件之一: 序列的 ...
- Repeats SPOJ - REPEATS(重复次数最多的连续重复子串)
论文题例8 https://blog.csdn.net/queuelovestack/article/details/53031731这个解释很好 其实,当枚举的重复子串长度为i时,我们在枚举r[i* ...
- 从商用到开源:15个维度,全面剖析DB2与MySQL数据库的差异
随着MySQL数据库的应用越来越广泛,DB2向MySQL数据库的迁移需求也越来越多.进行数据库之间迁移的时候,首先遇到的并且也是最基本最重要的就是两种数据库数据类型之间的转换. 相关阅读: 从商用到开 ...
- 【poj1743】 Musical Theme
http://poj.org/problem?id=1743 (题目链接) 题意 给出n个音符的乐谱,求其中不重叠的重复最长连续段,重复连续段的定义为两段音符起伏相同. Solution 论文题,相邻 ...
- Idea的Maven使用
使用SSM(Spring,SpringMVC和Mybatis) 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johns ...