当前位置:网站首页>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

边栏推荐
- Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server
- proteus仿真arduino中调用DHT11/22温湿度传感器
- CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
- Learn more about xxE injection
- Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future
- Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
- JUC工具包学习
- 钉钉报警工具
- Security-001
- 使用灰度滤镜
猜你喜欢

Preliminary understanding of Panda3D audio and advanced interactive components

CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021

LANproxy mapping local development environment
![[number recognition] recognize 0-9 numbers based on Hopfield neural network with matlab code](/img/10/de712b67a7538be03f8fb722271949.png)
[number recognition] recognize 0-9 numbers based on Hopfield neural network with matlab code
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel

C#委托用法--控制台项目,通过委托实现事件

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

Find and leverage xxE – XML external entity injection

The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D

技术认证 | 图扑软件携手华为云再创合作共赢新局面
随机推荐
How to quickly view the API properties and usage of the h.265 video player easyplayer?
Simple and practical data visualization cases
并发和并行有什么区别?
How do I shut down oracle?
Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
ZCMU--1720: 死亡如风,我要装逼
The print version of imeta | international standard ISSN is officially confirmed, and the application for dual ISSN is completed
QT with OpenGL(Shadow Mapping)(平行光篇)
【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码
CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
Common Taylor expansion
加速IGBT国产化!比亚迪半导体将独立上市,市值或达300亿元!
【数字识别】基于知识库实现手写体数字识别附matlab代码
The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D
How to use xshell Free Edition
Interviewer: let's talk about the specific process of network data transmission
迪赛智慧数——其他图表(平行坐标图):家庭未来资产配置意愿
日产1500万只!比亚迪口罩拿下美国加州10亿美元订单
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步