C#语言进阶(二)—事件全解

简介: C#语言进阶(二)—事件全解

事件

事件修饰符: virtual , override , abstract , sealed

解释:

  广播者是声明委托并使用的类,它控制什么时候使用委托。

  订阅者就是给广播类声明的委托,使用 += 或 -= 改变委托数量的这一操作过程者。

用法:声明事件就是在声明委托时前面加上 event 关键字。

作用:事件的作用是 让委托的广播者和订阅者的 结构更清晰。

过程:编译器看到这个委托添加了事件关键字,会自动让委托变成私有的,然后给它添加对应的访问器类似类属性的{set;get;},这样订阅者就只能通过访问器来修改自己的委托。

  所以,简单来说,事件就是把委托默认设置为私有,然后创建一个默认的访问器让外部只能通过访问器+=或-=修改委托的成员,且不能将这个委托置null。

1.基本用法 发布订阅模式

#region 普通的  发布订阅模式
public delegate void ScoreChangeHandle(decimal oldScore, decimal newScore);
//广播者
internal class BroadCaster
{
   
    private string? name;
    private decimal score;

    public event ScoreChangeHandle? ScoreChangedEvent;

    public BroadCaster(string name)
    {
   
        this.name = name;
    }
    public decimal Score
    {
   
        get {
    return score;}
        set
        {
   
            if (score == value) return;
            decimal oldScore = score;
            score = value;
            ScoreChangedEvent?.Invoke(oldScore, score);
        }
    }
}
//订阅者
internal class Subscriber
{
   
    private readonly string _id;
    public Subscriber(string id, BroadCaster broad)
    {
   
        _id = id;
        broad.ScoreChangedEvent += ScoreChange;
    }

    void ScoreChange(decimal oldScore, decimal newScore)
    {
   
        Console.WriteLine("this id is: "+_id+",  oldscore is " + oldScore + "  ,new Score is: " + newScore+"  ,time is: "+ DateTime.Now);
    }
}
#endregion

static void Main(string[] args)
{
   
    BroadCaster bc = new BroadCaster("bc1");
    Subscriber sub1 = new Subscriber("01", bc);
    Subscriber sub2 = new Subscriber("02", bc);
    bc.Score = 10;
}

//输出
this id is: 01,  oldscore is 0  ,new Score is: 10  ,time is: 2024/4/18 16:19:33
this id is: 02,  oldscore is 0  ,new Score is: 10  ,time is: 2024/4/18 16:19:33

2. .net标准事件模型

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

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

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

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

#region .net Framework 标准事件模型
//继承标准事件模型参数类型
//这个父类啥都没有,只有一个静态参数,一个构造方法,可以点进去看
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;
    }
}
//发布者
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);
        }
    }
}
//订阅者
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);
    }
}
#endregion

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;
}

//输出
this id is: 01,  oldscore is 0  ,new Score is: 15  ,time is: 2024/4/18 16:43:12
this id is: 02,  oldscore is 0  ,new Score is: 15  ,time is: 2024/4/18 16:43:12

3. 事件访问器

  默认情况下,编译器会默认实现事件的访问器,如果显示的去实现,那么编译器就不会自动取生成默认的访问器。

  从功能上,自己手动写访问器和编译器默认生成是一样的,但是编译器默认生成的使用了无锁的比较并交换算法,保证在更新委托时的线程安全性,在多线程情况下更安全些。

  一般情况不需要自己去显示写事件访问器,如果需要更多高级操作,那么就不得不用。

  以下是一些可能需要用的情况 (当然也可以不用):

  • 当事件很多,订阅者却不多,这种情况下需要自定义访问器的add方法,在方法里使用字典来存储委托引用,这样比原来的开销小
  • 当广播类继承了多个事件接口,并且有多个接口的事件名称是相同的,那么需要把这些接口的事件显示的去创建访问器,用来区分它们用哪个访问器访问
#region 继承多个接口 显示创建访问器
public interface IMathScore
{
   
    event EventHandler<ScoreChangedCusEventArgs> ScoreChanged;
}
public interface IEnglishScore
{
   
    event EventHandler<ScoreChangedCusEventArgs> ScoreChanged;
}

public class ScoreChangedCusEventArgs : EventArgs
{
   
    public static readonly new ScoreChangedCusEventArgs? Empty;
    //通常标准事件模型传递的参数设置为只读类型
    public readonly decimal oldScore;
    public readonly decimal newScore;
    public ScoreChangedCusEventArgs(decimal oldScore, decimal newScore)
    {
   
        this.oldScore = oldScore;
        this.newScore = newScore;
    }
}
//发布者
public class BroadCasterCusStandar:IMathScore,IEnglishScore
{
   
    private string? name;
    private decimal englishScore;
    private decimal mathScore;

    event EventHandler<ScoreChangedCusEventArgs> MathEvent;
    event EventHandler<ScoreChangedCusEventArgs> EnglishEvent;

    object objectLock = new Object();

    event EventHandler<ScoreChangedCusEventArgs>? IMathScore.ScoreChanged
    {
   
        add {
   
            lock (objectLock)
            {
   
                MathEvent += value;
            }
        }
        remove {
   
            lock (objectLock)
            {
   
                MathEvent -= value;
            }
        }
    }

    event EventHandler<ScoreChangedCusEventArgs>? IEnglishScore.ScoreChanged
    {
   
        add
        {
   
            lock (objectLock)
            {
   
                EnglishEvent += value;
            }
        }
        remove
        {
   
            lock (objectLock)
            {
   
                EnglishEvent -= value;
            }
        }
    }

    protected virtual void OnMathScoreChanged(ScoreChangedCusEventArgs? e)
    {
   
        MathEvent?.Invoke(this, e);
    }
    protected virtual void OnEnglishScoreChanged(ScoreChangedCusEventArgs? e)
    {
   
        EnglishEvent?.Invoke(this, e);
    }

    public BroadCasterCusStandar(string name)
    {
   
        this.name = name;
    }
    public decimal MathScore
    {
   
        get {
    return mathScore; }
        set
        {
   
            if (mathScore == value) return;
            decimal oldMathScore = mathScore;
            mathScore = value;

            OnMathScoreChanged(new ScoreChangedCusEventArgs(oldMathScore, mathScore));
            //如果不需要传值,那么可以用下面代替
            //OnMathScoreChanged(ScoreChangedCusEventArgs.Empty);
        }
    }

    public decimal EnglishScore
    {
   
        get {
    return englishScore; }
        set
        {
   
            if (englishScore == value) return;
            decimal oldEnglishScore = englishScore;
            englishScore = value;

            OnEnglishScoreChanged(new ScoreChangedCusEventArgs(oldEnglishScore, englishScore));
            //如果不需要传值,那么可以用下面代替
            //OnEnglishScoreChanged(ScoreChangedCusEventArgs.Empty);
        }
    }
}
//订阅者
internal class SubscriberCus1Standar
{
   
    private readonly string _id;
    public SubscriberCus1Standar(string id, BroadCasterCusStandar broad)
    {
   
        _id = id;
        IEnglishScore englishBroad = (IEnglishScore)broad;
        //订阅信息
        englishBroad.ScoreChanged += ScoreChanged;
    }

    //处理广播信息
    void ScoreChanged(object? obj, ScoreChangedCusEventArgs e)
    {
   
        if (e == ScoreChangedCusEventArgs.Empty)
        {
   
            return;
        }
        Console.WriteLine("this id is: " + _id + ",  oldscore is " + e.oldScore + "  ,new Score is: " + e.newScore + "  ,time is: " + DateTime.Now);
    }
}
internal class SubscriberCus2Standar
{
   
    private readonly string _id;
    public SubscriberCus2Standar(string id, BroadCasterCusStandar broad)
    {
   
        _id = id;
        IMathScore englishBroad = (IMathScore)broad;
        //订阅信息
        englishBroad.ScoreChanged += ScoreChanged;
    }

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

//输出
this id is: 02,  oldscore is 0  ,new Score is: 15  ,time is: 2024/4/18 17:34:35
this id is: 01,  oldscore is 0  ,new Score is: 20  ,time is: 2024/4/18 17:34:35
目录
相关文章
|
2月前
|
存储 开发框架 .NET
C#语言究竟隐藏了哪些秘密?一文带你揭开编程界的神秘面纱
【8月更文挑战第22天】C#是微软推出的面向对象编程语言,以其简洁的语法和强大的功能,在软件开发领域占据重要地位。作为一种强类型语言,C#确保了代码的可读性和可维护性。它支持多种数据类型,如整型、浮点型及复合类型如类和结构体。类是核心概念,用于定义对象的属性和行为。C#还包括方法、异常处理、集合类型如列表和字典,以及泛型和LINQ等高级特性,支持异步编程以提高应用响应性。.NET Core的推出进一步增强了C#的跨平台能力。
67 3
|
2天前
|
JSON C# 开发者
C#语言新特性深度剖析:提升你的.NET开发效率
【10月更文挑战第15天】C#语言凭借其强大的功能和易用性深受开发者喜爱。随着.NET平台的演进,C#不断引入新特性,如C# 7.0的模式匹配和C# 8.0的异步流,显著提升了开发效率和代码可维护性。本文将深入探讨这些新特性,助力开发者在.NET开发中更高效地利用它们。
9 1
|
14天前
|
存储 开发框架 .NET
C#语言如何搭建分布式文件存储系统
C#语言如何搭建分布式文件存储系统
47 2
|
1月前
|
前端开发 Java C#
C#语言的优缺点?
C#语言的优缺点?
65 3
|
1月前
|
安全 IDE Java
C#语言的
C#语言是一种面向对象的编程语言
22 1
|
1月前
|
IDE C# 开发工具
C# 语言的主要优势是什么?
C# 语言的主要优势是什么?
60 2
|
1月前
|
C#
C#一分钟浅谈:委托与事件的实现方式
本文详细介绍了C#编程中委托与事件的基础知识及应用场景。首先解释了委托的概念,包括定义与使用方法;接着介绍了事件这一基于委托的特殊类型,展示了如何在类中定义事件及跨类订阅与处理事件;最后讨论了常见问题如事件未处理异常、重复订阅及内存泄漏等,并提出了相应的解决方案。通过本文,读者将全面掌握委托与事件的使用技巧,提升应用程序的设计与开发水平。
80 7
|
1月前
|
监控 安全 C#
C# 语言助力员工监控系统的完善
在数字化时代,企业日益重视员工管理的效率与精准度,员工监控系统因此成为提升管理水平的有效工具。C# 语言凭借其简洁、高效和安全的特点,在开发此类系统中扮演了重要角色,可实现实时监控员工电脑操作、网络行为及工作时间统计等功能,从而提高工作效率并保障企业利益。同时,企业在应用这些技术时也需关注员工隐私权的保护。
24 6
|
2月前
|
JSON C# 开发者
💡探索C#语言进化论:揭秘.NET开发效率飙升的秘密武器💼
【8月更文挑战第28天】C#语言凭借其强大的功能与易用性深受开发者喜爱。伴随.NET平台演进,C#持续引入新特性,如C# 7.0的模式匹配,让处理复杂数据结构更直观简洁;C# 8.0的异步流则使异步编程更灵活高效,无需一次性加载全部数据至内存。通过示例展示了模式匹配简化JSON解析及异步流实现文件逐行读取的应用。此外,C# 8.0还提供了默认接口成员和可空引用类型等特性,进一步提高.NET开发效率与代码可维护性。随着C#的发展,未来的.NET开发将更加高效便捷。
53 1
|
2月前
|
C#
由浅入深理解C#中的事件
由浅入深理解C#中的事件
106 19