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 小时前
15.C# —— 多接口,泛型方法,泛型效率、泛型类和泛型接口、泛型约束
c#·上位机
影寂ldy2 小时前
C# const 常量 / readonly 只读 / static readonly
java·开发语言·c#
iCxhust3 小时前
c#多串口重量采集上位机程序
开发语言·汇编·c#·微机原理·8088单板机
雪豹阿伟3 小时前
14.C# —— 虚方法,new/override,密封类,索引器,接口
c#·上位机
light blue bird4 小时前
支组汇总主子节点工序路径图表
前端·jvm·.net·桌面端·gdi绘图
糖果店的幽灵4 小时前
LangChain 1.3 完全教程:从入门到精通-Part 11: Tools(工具系统)
开发语言·langchain·c#
专注VB编程开发20年8 小时前
python翻译网页HTML的难题
python·c#·html
z落落9 小时前
C# 抽象类(abstract)
java·开发语言·c#
unityのkiven10 小时前
工作分享1(26.5.27):基于栈实现全局返回逻辑通用架构设计(适配异步 + 确认弹窗)
游戏·unity·c#·客户端架构