How can I make my own event in C#?

推荐答案

Here's an example of creating and using an event with C#

using System;

namespace Event_Example
{
    //First we have to define a delegate that acts as a signature for the
    //function that is ultimately called when the event is triggered.
    //You will notice that the second parameter is of MyEventArgs type.
    //This object will contain information about the triggered event.
    public delegate void MyEventHandler(object source, MyEventArgs e);

    //This is a class which describes the event to the class that recieves it.
    //An EventArgs class must always derive from System.EventArgs.
    public class MyEventArgs : EventArgs
    {
        private string EventInfo;
        public MyEventArgs(string Text)
        {
            EventInfo = Text;
        }
        public string GetInfo()
        {
            return EventInfo;
        }
    }

    //This next class is the one which contains an event and triggers it
    //once an action is performed. For example, lets trigger this event
    //once a variable is incremented over a particular value. Notice the
    //event uses the MyEventHandler delegate to create a signature
    //for the called function.
    public class MyClass
    {
        public event MyEventHandler OnMaximum;
        private int i;
        private int Maximum = 10;
        public int MyValue
        {
            get
            {
                return i;
            }
            set
            {
                if(value <= Maximum)
                {
                    i = value;
                }
                else
                {
                    //To make sure we only trigger the event if a handler is present
                    //we check the event to make sure it's not null.
                    if(OnMaximum != null)
                    {
                        OnMaximum(this, new MyEventArgs("You've entered " +
                            value.ToString() +
                            ", but the maximum is " +
                            Maximum.ToString()));
                    }
                }
            }
        }
    }

    class Program
    {
        //This is the actual method that will be assigned to the event handler
        //within the above class. This is where we perform an action once the
        //event has been triggered.
        static void MaximumReached(object source, MyEventArgs e)
        {
            Console.WriteLine(e.GetInfo());
        }

        static void Main(string[] args)
        {
            //Now lets test the event contained in the above class.
            MyClass MyObject = new MyClass();
            MyObject.OnMaximum += new MyEventHandler(MaximumReached);

            for(int x = 0; x <= 15; x++)
            {
                MyObject.MyValue = x;
            }

            Console.ReadLine();
        }
    }
}

.net相关问答推荐

将Visual Studio更新到v17.9.3后,IDE关闭,dotnet.exe命令报告致命错误.内部CLR错误.(0x80131506)

带有ASP.NET核心的Angular 项目模板.API试验

使用 DataDog 收集 OpenTelemetry 跟踪

如何获取控制台应用程序的执行目录

比较 C# 中的字符串和对象

每第 N 个字符/数字拆分一个字符串/数字?

为什么递归调用会导致不同堆栈深度的 StackOverflow?

数据库架构更改后更新 LINQ to SQL 类的最佳方法

如何正确停止BackgroundWorker

DBNull 的意义何在?

是否可以更改 Winforms 组合框以禁用输入?

无法将文件 *.mdf 作为数据库附加

我们应该总是在类中包含一个默认构造函数吗?

使用+运算符的字符串连接

绑定到不在列表中的值的可编辑组合框

我应该如何删除 DbSet 中的所有元素?

String.Join 与 StringBuilder:哪个更快?

.NET 图形库?

判断任意字符串是否为有效文件名的最简单方法

.NET Remoting 真的被弃用了吗?