当前位置:网站首页>C# 事件用法案例 订阅事件+=
C# 事件用法案例 订阅事件+=
2022-07-27 07:26:00 【济南医疗小程序状元】
C# 事件用法
using System;
namespace WeiTuo
{
class Program
{
// SayUpdate 类型的事件. 声明了1个事件。
public static event SayUpdate SayHelloEvent;
// 带一个参数的委托 并且无返回值的委托
public delegate void SayUpdate(string name);
static void Main(string[] args)
{
// 委托的声明 委托的使用方法 委托的解释 将方法以变量的形式传递,并且以方法的形式执行。
//
SayUpdate dlg = SayHi;
dlg += SayBye;
dlg -= SayBye;
//dlg("语音,你好啊");
// 匿名函数
// 事件的使用方法 += -= 1 用+= 订阅事件 事件有2个方法,一个+= 一个-=方法。
// 注册 订阅事件。
SayHelloEvent += Program_SayHelloEvent;
// 判断事件是不是存在。不为空。
if(SayHelloEvent != null)
{
SayHelloEvent("老王"); // 调用事件
}
Console.ReadLine();
}
private static void Program_SayHelloEvent(string name)
{
//throw new NotImplementedException();
Console.WriteLine($"{name},我是事件!");
}
public static void SayHi(string name)
{
Console.WriteLine($"{ name},你好啊 语音技术");
}
public static void SayBye(string name)
{
Console.WriteLine($"{ name},再见");
}
}
}
效果

边栏推荐
- HU相关配置
- [golang learning notes 2.0] arrays and slices in golang
- Oracle cleans up the Database disk space of tables with referenced partitions
- Flink principle (I) TTL management and fault tolerance mechanism of state
- The DrawImage method calls the solution of not displaying pictures for the first time
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- js正则表达式实现每三位数字加一个逗号
- Understanding and learning of properties class and properties configuration file
- Plato farm is expected to further expand its ecosystem through elephant swap
- IO中节点流和处理流的理解学习
猜你喜欢
随机推荐
国内首款开源MySQL原生HTAP数据库即将发布!三大亮点抢先看,限量周边等你来~
Understanding and learning of properties class and properties configuration file
HU相关配置
Install tensorflow
shell awk相关练习
Expose Prometheus metrics in Perl programs
MySQL backup strategy
Docker install MySQL 8.0.28
STM32_找到导致进入HardFault_Handler的函数
Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
Shell loop exercise
Codeforces Round #810 (Div.2) A-C
The solution of using sqlplus to display Chinese as garbled code
小程序消息推送配置 Token校验失败,请检查确认
Showdoc vulnerability learning - cnvd-2020-26585 (arbitrary file upload)
实用的新药研发项目管理平台
The first open source MySQL native HTAP database in China will be released soon! Look at the three highlights first, limited to the surrounding areas, waiting for you~
flink去重(一)flink、flink-sql中常见的去重方案总结
STM32_ Find the cause of entering hardfault_ Handler's function
Multi condition query of when









