首页 > 其他分享 >自定义委托01

自定义委托01

时间:2023-12-14 19:33:41浏览次数:26  
标签:01 自定义 委托 System CustomEventHandler CustomEvent EventClass my public

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eventTest20231209
{
class Program
{
static void Main(string[] args)
{
EventClass my = new EventClass();
my.CustomEvent += new EventClass.CustomEventHandler(TestClass.CustomEvent1);
my.CustomEvent += new EventClass.CustomEventHandler(TestClass.CustomEvent2);
my.CustomEvent += new EventClass.CustomEventHandler(TestClass.CustomEvent3);
my.InvokeEvent();
System.Threading.Thread.Sleep(1000);
my.CustomEvent -= new EventClass.CustomEventHandler(TestClass.CustomEvent2);
my.InvokeEvent();
Console.ReadLine();


}
}

public class EventClass //事件的拥有者
{
//(1)声明一个委托类型
public delegate void CustomEventHandler(object sender, EventArgs e);
//(2)用委托类型声明事件
public event CustomEventHandler CustomEvent;
public void InvokeEvent()
{
if (CustomEvent != null)//判断事件是否有对象注册了
{
CustomEvent(this, EventArgs.Empty);//调用事件
}
}

 

}

public class TestClass
{
public static void CustomEvent1(object sender, EventArgs e)
{
Console.WriteLine("Fire Event1 is {0}", DateTime.Now);
}
public static void CustomEvent2(object sender, EventArgs e)
{
Console.WriteLine("Fire Event2 is {0}", DateTime.Now);
}
public static void CustomEvent3(object sender, EventArgs e)
{
Console.WriteLine("Fire Event3 is {0}", DateTime.Now);
}

}
}

标签:01,自定义,委托,System,CustomEventHandler,CustomEvent,EventClass,my,public
From: https://www.cnblogs.com/luolele/p/17901833.html

相关文章

  • 连接MySQL报错{"Authentication to host 'PC10103' for user 'root' using method &#
    连接MySQL报错{"Authenticationtohost'PC10103'foruser'root'usingmethod'sha256_password'failedwithmessage:Accessdeniedforuser'root'@'PC10103'(usingpassword:YES)"}先在MySQL中执行如下查询SEL......
  • php tp框架 自定义日志
    调用方法$file_log=['order_id'=>123,];(newLogs('log'))->infos('日志文案',$file_log);[2023-12-1415:24:13][INFO][log]{"msg":"日志文案","params":{"order_id":123},"file......
  • 01_前言和学习方法介绍
    01_前言和学习方法介绍ARM裸机程序系统结构图应用层驱动层硬件层类Android等复杂功能系统结构图(有OS)ApplicationsKernelDriverH/W学习内容交叉编译环境搭建bootloader功能子系统内核核心子系统文件系统子系统学习思路和方法先整体后局部,层层推进如......
  • 01_ARM学习准备工作
    01_ARM学习准备工作熟悉Tiny210开发ARM9-2410ARM11-6410CortexA8-Tiny210CortexA15...1.开始进入到真正的嵌入式阶段1.1.理解一下我们要学的内容启动过程1、上电2、从BIOS里读引导信息3、bootloader:准备运行环境,引导操作系统3、操作系统kernerlinit4......
  • NetSuite 开发日记:如何管理多环境自定义列表值
    在NetSuite中可以创建自定义列表,列表可用于为其他(自定义)记录上的下拉选项列表值。varrec=record.create({type:'customrecord_xx'});rec.setValue({fieldId:'custrecord_xx_fld',value:'1'});rec.save();我们设置自定义列表值,需要使用该值的内......
  • 【工作日记(实习)01】实习45天小结
    工作小结  来某科研院所实习45天了。从10月的最后一天了,到今天12月14日。  看这里的工作,其实也还可以。工作不是说是忙,倒不如说是杂。凭我的专业知识能力,基本上可以说是没问题的。  早上8:30上班,中午11:40开始去吃饭。下午14:00到14:30上班(一般是14:30,因为去早了,办公室可......
  • 101. 对称二叉树
    1.题目介绍给你一个二叉树的根节点\(root\),检查它是否轴对称。示例1:输入:root=[1,2,2,3,4,4,3]输出:true示例2:输入:root=[1,2,2,null,3,null,3]输出:false提示:树中节点数目在范围\([1,1000]\)内\(-100<=Node.val<=100\)进阶:你可以运用递归和迭代两种......
  • P4463 [集训队互测 2012] calc 题解
    Description一个序列\(a_1,a_2,\dots,a_n\)是合法的,当且仅当:\(a_1,a_2,\dots,a_n\)都是\([1,k]\)中的整数。\(a_1,a_2,\dots,a_n\)互不相等。一个序列的值定义为它里面所有数的乘积,即\(a_1\timesa_2\times\dots\timesa_n\)。求所有不同合法序列的值的和对\(p\)......
  • MUI增加自定义icon图标
    mui框架遵循极简原则,在icon图标集上也是如此,mui仅集成了原生系统中最常用的图标;使用icon图标集的优点:多个图标字体合成一个字体文件,避免每张图片都需要联网请求;字体可任意缩放,而图片放大会失真、缩小则浪费像素;可通过css任意改变颜色、设置阴影及透明效果;一、操作方法:(1)找到任意矢量......
  • CF301D Yaroslav and Divisors
    因为是排列,所以数对总数是调和级数的\(O(n\logn)\),可以暴力枚举。容斥,区间左右端点均在\([l,r]\)中的数对数量等于左右端点均在\([1,r]\)中的数对数量减去左右端点均在\([1,l-1]\)中的数对数量,再减去左端点在\([1,l-1]\)中且右端点在\([l,r]\)中的数对数量。发现前......