当前位置:网站首页>C # basic 1-events and commissions
C # basic 1-events and commissions
2022-07-28 20:54:00 【W.C.Zeng】
entrust
Delegate is simply understood as a variable of function type ( Reference type ), Represents a reference to a method .
Website to explain delegate
Define delegate type
public delegate void KeyDelegate(string key);
Define the processing function of the corresponding delegate type
public static void PrintString(string key)
{
Console.WriteLine(key);
}
Instantiate variables of delegate type
KeyDelegate keyAevent = new KeyDelegate(DelegateFunc.PrintString);
A delegate can bind multiple event handlers
multiEvent += new KeyDelegate(DelegateFunc.PrintString2);
Use delegate variables , Call all the event handling functions corresponding to the delegate
multiEvent("B");
The difference between delegation and interface
Definition
Interface (interface) It is a set that constrains what functions a class should have , Constrains which methods a class should have , It is convenient for class management and extension , Using interfaces can also solve the single inheritance problem of classes .
entrust (delegate) Is a collection of methods , Using delegates, you can operate on this set of methods , Such as += .
Applicable scenario
Use the interface in the following cases :
- Has inherited a class , You can no longer inherit other classes , But I hope to continue to expand other functions
- When completely abstract
- When multi person collaboration requires the function of constraint class
Use delegates when :
- In the first two processes of event processing : Defining events ( Commissioned by using )、 The binding event ( The method set corresponding to the operation delegate )、 Event handler 、 Call event
event
Event is a package of delegation
Events are variables of delegate type
Events use Release - subscribe Model of , The publisher contains Event and delegate definitions The connection between the event and the Commission , Subscriber accept Event and provide event handling
Declaration delegation
public delegate void KeyDelegate(string key);
Claim event
public static event KeyDelegate? keyEvent;
The binding event
EventHandler.keyEvent += DelegateFunc.PrintString2;
Define event handling functions ( Corresponding to the delegate type )
public static void PrintString2(string key)
{
Console.WriteLine("-" + key + "-");
}
Call event
keyEvent?.Invoke(key);
Add
Another kind public static event Action<string> keyEvent; The event definition of , among Action<T> Is the abbreviation of delegation , You can quickly declare event variables of delegate types , Unlike the traditional entrusted writing , You need to declare the delegate type first , Then instantiate a delegate variable with the delegate type to use
Complete code
using System;
// 1.1 Declare a delegate type KeyDelegate
public delegate void KeyDelegate(string key);
class DelegateFunc
{
// 1.2 Declare the method corresponding to the delegate type Return value 、 Parameter list and delegate type KeyDelegate bring into correspondence with
public static void PrintString(string key)
{
Console.WriteLine(key);
}
// 2.3 Event handler
public static void PrintString2(string key)
{
Console.WriteLine("-" + key + "-");
}
}
public static class EventHandler
{
// 2.1 Before declaring the event , A delegate type must be declared in advance for example KeyDelegate
public static event KeyDelegate? keyEvent;
// 2.4 Call event
public static void CallKeyEvent(string key)
{
keyEvent?.Invoke(key);
}
}
class Program
{
static void Main()
{
// 1.3 Instantiate a variable of delegate type keyAevent , Corresponding to the delegated function DelegateFunc.PrintString
KeyDelegate keyAevent = new KeyDelegate(DelegateFunc.PrintString);
KeyDelegate multiEvent;
multiEvent = keyAevent;
// 1.4 A delegate can use += The operator Bind multiple event handlers
multiEvent += new KeyDelegate(DelegateFunc.PrintString2);
// 2.2 The binding event
EventHandler.keyEvent += DelegateFunc.PrintString2;
while (true)
{
ConsoleKeyInfo key = Console.ReadKey();
switch (key.Key)
{
case ConsoleKey.A:
// 1.4 aA
keyAevent("A");
break;
case ConsoleKey.B:
// 1.4 bB
// 1.4 -B-
multiEvent("B");
break;
case ConsoleKey.C:
// 2.4 c-C-
EventHandler.CallKeyEvent("C");
break;
case ConsoleKey.Q:
Console.Clear();
return;
default:
Console.WriteLine();
break;
}
}
}
}
边栏推荐
- 一个程序员的水平能差到什么程度?尼玛,都是人才呀...
- How to use redis to realize things and locks?
- 融合数据库生态:利用 EventBridge 构建 CDC 应用
- SQL审核工具自荐Owls
- Hangao database best practice configuration tool Hg_ BP log collection content
- Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters
- The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution
- 阿里云 MSE 支持 Go 语言流量防护
- 到底为什么不建议使用SELECT * ?
- Space shooting Lesson 15: props
猜你喜欢

漂亮的蓝色背景表单输入框样式

作业 ce
About the title of linking to other pages

一个程序员的水平能差到什么程度?尼玛,都是人才呀...

UE4 3dui widget translucent rendering blur and ghosting problems

企业如何成功完成云迁移?

Prize essay solicitation | 2022 cloud native programming challenge draft activity opens

PXE_ KS unattended system

js图片悬挂样式照片墙js特效

JS picture hanging style photo wall JS special effect
随机推荐
Introduction to redis I: redis practical reading notes
H5 wechat shooting game source code
企业如何成功完成云迁移?
PL515 SOT23-5 单/双口 USB 充电协议端口控制器 百盛电子代理商
How can enterprises successfully complete cloud migration?
Space shooting Lesson 15: props
卡通js射击小游戏源码
JS fly into JS special effect pop-up login box
leetcode:2141. 同时运行 N 台电脑的最长时间【最值考虑二分】
融合数据库生态:利用 EventBridge 构建 CDC 应用
Space shooting Lesson 13: explosion effect
System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
Talking about canvas and three rendering modes in unity
C# 读取 CSV文件内数据,导入DataTable后显示
#yyds干货盘点# 面试必刷TOP101:链表中的节点每k个一组翻转
JS picture hanging style photo wall JS special effect
Redis 3.0 source code analysis - data structure and object SDS list Dict
JS win7 transparent desktop switching background start menu JS special effect
Fragment中使用ViewPager滑动浏览页面
C reads the data in the CSV file and displays it after importing the DataTable