C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Event事件

C#中Event事件的实现示例

作者:ghost143

事件是委托的封装,用于解耦对象交互,支持多播通知,可传递参数,本文主要介绍了C#中Event事件的实现示例,具有一定的参考价值,感兴趣的可以了解一下

什么是事件?

定义

用途

和委托关系 

事件的组成部分

基本语法

1. 定义事件

public delegate void NotifyEventHandler(object sender, EventArgs e);

public class Process
{
    // 使用委托类型定义事件
    public event NotifyEventHandler ProcessCompleted;

    public void StartProcess()
    {
        Console.WriteLine("Process started.");
        
        // 模拟过程完成后引发事件
        OnProcessCompleted(EventArgs.Empty);
    }

    protected virtual void OnProcessCompleted(EventArgs e)
    {
        ProcessCompleted?.Invoke(this, e);
    }
}

2. 订阅和触发事件 

public class Program
{
    public static void Main()
    {
        Process process = new Process();
        
        // 订阅事件
        process.ProcessCompleted += Process_ProcessCompleted;
        
        process.StartProcess();
    }

    private static void Process_ProcessCompleted(object sender, EventArgs e)
    {
        Console.WriteLine("Process completed event received.");
    }
}

说明:

高级主题 

内置事件处理器

public event EventHandler ProcessCompleted;
public class CustomEventArgs : EventArgs
{
    public string Message { get; set; }
}

public event EventHandler<CustomEventArgs> ProcessCompletedWithMessage;

 使用内置EventHandler

public class SimpleProcess
{
    public event EventHandler ProcessCompleted;

    public void CompleteProcess()
    {
        Console.WriteLine("Completing process...");
        ProcessCompleted?.Invoke(this, EventArgs.Empty);
    }
}

自定义事件参数

public class DownloadEventArgs : EventArgs
{
    public string FileName { get; set; }
    public long FileSize { get; set; }
}

public class DownloadManager
{
    public event EventHandler<DownloadEventArgs> DownloadCompleted;

    public void CompleteDownload(string fileName, long fileSize)
    {
        Console.WriteLine($"Download completed: {fileName}, Size: {fileSize}");
        OnDownloadCompleted(new DownloadEventArgs { FileName = fileName, FileSize = fileSize });
    }

    protected virtual void OnDownloadCompleted(DownloadEventArgs e)
    {
        DownloadCompleted?.Invoke(this, e);
    }
}

 事件的多播特性

事件是基于委托的,因此支持多播,即多个订阅者可以同时接收同一事件通知。

public static void HandleSuccess(object sender, EventArgs e)
{
    Console.WriteLine("Success handler executed.");
}

public static void Main()
{
    SimpleProcess sp = new SimpleProcess();
    
    // 多个方法订阅同一事件
    sp.ProcessCompleted += Process_ProcessCompleted;
    sp.ProcessCompleted += HandleSuccess;
    
    sp.CompleteProcess();
}

实践习题

1.创建一个类DownloadManager,定义一个DownloadCompleted事件。在下载完成后触发此事件,并在主程序中订阅和处理该事件。

using System;

public class DownloadManager
{
    public event EventHandler DownloadCompleted;

    public void StartDownload()
    {
        Console.WriteLine("Starting download...");

        // Simulate download completion
        OnDownloadCompleted(EventArgs.Empty);
    }

    protected virtual void OnDownloadCompleted(EventArgs e)
    {
        DownloadCompleted?.Invoke(this, e);
    }
}

public class Program
{
    public static void Main()
    {
        DownloadManager dm = new DownloadManager();
        
        // 订阅事件
        dm.DownloadCompleted += Download_Completed;
        
        dm.StartDownload();
    }

    private static void Download_Completed(object sender, EventArgs e)
    {
        Console.WriteLine("Download completed event received.");
    }
}

2.修改上述DownloadManager,使得DownloadCompleted事件携带下载的文件名和大小信息。

using System;

public class DownloadEventArgs : EventArgs
{
    public string FileName { get; set; }
    public long FileSize { get; set; }
}

public class DownloadManager
{
    public event EventHandler<DownloadEventArgs> DownloadCompleted;

    public void StartDownload(string fileName, long fileSize)
    {
        Console.WriteLine($"Starting download of {fileName}...");

        // Simulate download completion
        OnDownloadCompleted(new DownloadEventArgs { FileName = fileName, FileSize = fileSize });
    }

    protected virtual void OnDownloadCompleted(DownloadEventArgs e)
    {
        DownloadCompleted?.Invoke(this, e);
    }
}

public class Program
{
    public static void Main()
    {
        DownloadManager dm = new DownloadManager();
        
        // 订阅事件
        dm.DownloadCompleted += Download_Completed;
        
        dm.StartDownload("example.txt", 1024);
    }

    private static void Download_Completed(object sender, DownloadEventArgs e)
    {
        Console.WriteLine($"Download completed: {e.FileName}, Size: {e.FileSize}");
    }
}

以上内容涵盖了如何定义、使用和扩展事件。更多相关C# Event事件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文