C#语言进阶(二)—事件 第二篇(.net标准事件模型)

总目录
C# 语法总目录
系列链接
C#语言进阶(二) 事件 第一篇(发布订阅模式)
C#语言进阶(二) 事件 第二篇(.net标准事件模型)
C#语言进阶(二) 事件 第二篇(事件访问器)

事件 第二篇目录

    • [事件 第二篇](#事件 第二篇)
      • [2. .net标准事件模型](#2. .net标准事件模型)

事件 第二篇

2. .net标准事件模型

  标准事件模型是 .net framwork 定义的一个标准。可用可不用,只是一个标准而已。

  官方为这个标准定义了一个事件参数类,用于给事件传递参数。这就是上面说的,这个模型可用可不用,不用官方的,自己也能做一个类似的,做个开发,没必要搞得这么复杂。

以下是上面案例根据标准事件模型的修改版本。

  这里使用 .net framwork的标准事件模型参数类: System.EventArgs 类,来模拟标准事件模型

标准事件参数类

csharp 复制代码
//继承标准事件模型参数类型
//这个父类啥都没有,只有一个静态参数,一个构造方法,可以点进去看
public class ScoreChangedEventArgs : EventArgs
{
    public static readonly new ScoreChangedEventArgs? Empty;
    //通常标准事件模型传递的参数设置为只读类型
    public readonly decimal oldScore;
    public readonly decimal newScore;
    public ScoreChangedEventArgs(decimal oldScore,decimal newScore)
    {
        this.oldScore = oldScore;
        this.newScore = newScore;
    }
}

发布者类

csharp 复制代码
//发布者
public class BroadCasterStandar
{
    private string? name;
    private decimal score;
    //事件标准委托
    public event EventHandler<ScoreChangedEventArgs>? ScoreChanged;

    protected virtual void OnScoreChanged(ScoreChangedEventArgs? e)
    {
        ScoreChanged?.Invoke(this, e);
    }

    public BroadCasterStandar(string name)
    {
        this.name = name;
    }
    public decimal Score
    {
        get { return score; }
        set
        {
            if (score == value) return;
            decimal oldScore = score;
            score = value;
            
            OnScoreChanged(new ScoreChangedEventArgs(oldScore, score));
            //如果不需要传值,那么可以用下面代替
            //OnScoreChanged(ScoreChangedEventArgs.Empty);
        }
    }
}

订阅者类

csharp 复制代码
//订阅者
internal class SubscriberStandar
{
    private readonly string _id;
    public SubscriberStandar(string id, BroadCasterStandar broad)
    {
        _id = id;
        //订阅信息
        broad.ScoreChanged += ScoreChanged;
    }

    //处理广播信息
    void ScoreChanged(object? obj, ScoreChangedEventArgs e)
    {
        if (e == ScoreChangedEventArgs.Empty)
        {
            return;
        }
        Console.WriteLine("this id is: " + _id + ",  oldscore is " + e.oldScore + "  ,new Score is: " + e.newScore + "  ,time is: " + DateTime.Now);
    }
}

主程序

csharp 复制代码
static void Main(string[] args)
{
    BroadCasterStandar bcs = new BroadCasterStandar("bcs");
    SubscriberStandar sbs1 = new SubscriberStandar("01", bcs);
    SubscriberStandar sbs2 = new SubscriberStandar("02", bcs);
    //广播信息
    bcs.Score = 15;
}

输出

csharp 复制代码
//输出
this id is: 01,  oldscore is 0  ,new Score is: 15  ,time is: 2000/1/1 16:43:12
this id is: 02,  oldscore is 0  ,new Score is: 15  ,time is: 2000/1/1 16:43:12

总目录
C# 语法总目录
系列链接
C#语言进阶(二) 事件 第一篇(发布订阅模式)
C#语言进阶(二) 事件 第二篇(.net标准事件模型)
C#语言进阶(二) 事件 第二篇(事件访问器)

相关推荐
下页、再停留2 小时前
【C#桌面客户端系列学习-15】生成可视化图表之折线图、柱状图、饼状图
开发语言·c#
波力海苔夹心脆6758 小时前
VisionMaster 两级位置修正实战:塑料外壳与金属件装配角度检测
经验分享·计算机视觉·c#·visionmaster·海康机器人·位置修正
小羊没烦恼!9 小时前
C#综合揭秘——Entity Framework 并发处理详解
java·开发语言·前端·javascript·数据库·mysql·c#
淡海水10 小时前
05-02-源码-Boehm GC标记阶段
算法·unity·c#·游戏引擎·gc
信可维10 小时前
AI 读 Word 表格总读错?先查合并单元格和表头这 3 处
开发语言·人工智能·c#·word·软件工程
半摆烂日常13 小时前
手机表单的左滑操作:触摸事件的处理细节
大数据·开发语言·人工智能·智能手机·https·c#
绛橘色的日落(。・∀・)ノ13 小时前
C#学习笔记
笔记·学习·c#
asdzx6713 小时前
C# 将 HTML 转换为 PDF:基于 Chrome 的转换方案
pdf·c#·html
1-1=014 小时前
.NET 实现后台任务
c#·.net·.netcore·quartz·hangfire
步步为营DotNet14 小时前
从分代预算到 DATAS:.NET GC 调优到底在调什么
c#·.net·内存管理·gc·datas