当前位置:网站首页>C # delegate usage -- console project, which implements events through delegation
C # delegate usage -- console project, which implements events through delegation
2022-07-27 23:37:00 【Jinan medical applet champion】
Console project , Implement events through delegation
using System;
namespace Delegate
{
internal class Program
{
#region Commissioned cases Enter numbers in the console , When the numbers are not equal , Two events are triggered .
// entrust Case study 0.1 Enter numbers in the console , When the numbers are not equal , Two events are triggered .
public class EventDemo
{
private int value;
public delegate void SpeechHandler();// 1 Define a declaration delegate SpeechHandler
public event SpeechHandler ChangeSpeech;// 2 Define events according to delegates ChangeSpeech
#region AddSpe
// 0.3 Methods defined in the event . Included in the event , The specific method code for executing the behavior action of the event .
protected virtual void AddSpe()
{
if(ChangeSpeech !=null)
{
ChangeSpeech();
}
else
{
Console.WriteLine(" Voice event triggers (1)!");
}
}
#endregion
#region Events trigger (2) Method GetSpe
protected virtual void GetSpe()
{
if(ChangeSpeech != null)
{
ChangeSpeech();
}
else
{
Console.WriteLine(" Events trigger (2)!");
}
}
#endregion
#region Event behavior Method 3
protected virtual void AddSpeech()
{
if (ChangeSpeech != null)
{
ChangeSpeech();
}
else
{
Console.WriteLine(" The event behavior successfully triggers execution (3)");
}
}
#endregion
#region Set the value GZHS The way Where parameter values can be set
public EventDemo(int n)
{
SetValue(n);
}
public void SetValue(int n)
{
if(value != n)
{
value = n;
AddSpe();
GetSpe();
// Custom events Method 3 You must call the method in the event , To execute the event ! Otherwise, the method in the event will not be executed .
AddSpeech();
}
}
#endregion
#region
#endregion
}
#endregion
#region Case study 2
// 1. Declare a delegate ( A function pointer )
delegate void delegateSpeech(int a, float b);
// delegate return type Delegate type name ( parameter list ):
2. Create delegate object
//public static delegateSpeech dT1;
//3. Create functions that conform to the delegate format ( The function that the pointer points to )
//public void Test(int a, float b)
//{
// a += 100;
// b *= 2.0;
// Console.WriteLine("a:{0},b:{1}", a, b);
//}
#endregion
#region
#endregion
#region
#endregion
static void Main(string[] args)
{
#region Case study 1
// Instantiation of delegate class object
EventDemo e = new EventDemo(0);
Console.WriteLine(" Please enter a number :");
while(true)
{
int i= int.Parse(Console.ReadLine());// Read the numbers and set
e.SetValue(i);
}
#endregion
// 4. Assign the function name to the delegate
//dT1 = new delegateSpeech(Test);
}
}
}
effect

边栏推荐
- 简单实用的数据可视化案例
- 三次握手的Socket交互流程
- 苹果发布新款iPhone SE:搭载A13仿生处理器,售价3299元起
- 华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
- Visual display method of machine learning project
- Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future
- 怎么使用C# Winform实现复制文件显示进度
- 习题 --- BFS
- 一位软件投资者的独白:我为什么不追逐快速增长的公司
- 小程序容器技术超有料,可以让移动研发效率大幅提升
猜你喜欢

【图像检测】基于Combined Separability Filter实现鼻孔和瞳孔等圆检测matlab源码

After returning to mixlab for three days, "creative team" cured my spiritual internal friction
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel

怎么使用C# Winform实现复制文件显示进度

Read an article to understand artificial neural network

深入了解 XXE 注射

用户画像在科技期刊微信公众号精准推送中的应用

Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future

Interviewer: let's talk about the specific process of network data transmission

华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
随机推荐
NDK 系列(6):说一下注册 JNI 函数的方式和时机
携手长江存储,江波龙推出全球最小扩展卡
Helm chart explanation and common commands: helm template / package / plugin
Redis 哈希Hash底层数据结构
面试官:说一下网络数据传输的具体流程
The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D
评价自动化测试优劣的隐性指标
Zabbix4.0 uses SNMP agent to monitor vcenter6.5
Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future
Zabbix4.0使用SNMP代理方式监控vcenter6.5
Apple releases new iPhone se: equipped with A13 bionic processor, priced from 3299 yuan
[signal denoising] signal denoising based on Kalman filter with matlab code
C#委托用法--控制台项目,通过委托实现事件
Implicit indicators for evaluating the advantages and disadvantages of automated testing
Using the optical fingerprint scheme under the huiding screen, Samsung Galaxy a71 5g is listed
钉钉报警工具
Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server
【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
Elk log analysis system installation and deployment
突发,微信重要通知