Top > C# > イベント

イベント



    public class EvenSample1
    {
        public delegate void OrgEventHandler(object sender, EventArgs e);
        public event OrgEventHandler? TestEvent;

        public void Test()
        {
            if (TestEvent != null) TestEvent(this, EventArgs.Empty);
        }
    }

    public class EventSampleTest(ITestOutputHelper output)
    {
        [Fact]
        public void a()
        {
            var a = new EvenSample1();
            a.TestEvent += (sender, e) =>
            {
                Assert.Equal(a, sender);
            };
            a.Test();
        }
    }

組み込みに EventHandler<TEventArgs> デリゲートがあるので、自身でデリゲートを定義する必要は基本的にない。



    public class EvenSample2
    {
        public event EventHandler<EventArgs>? TestEvent;

        public void Test()
        {
            if (TestEvent != null) TestEvent(this, EventArgs.Empty);
        }
    }

    public class EventSampleTest(ITestOutputHelper output)
    {
        [Fact]
        public void b()
        {
            var a = new EvenSample2();
            a.TestEvent += (sender, e) =>
            {
                Assert.Equal(a, sender);
            };
            a.Test();
        }
    }

EventArgsはプロパティを持たない空のクラスなので、 引数を追加したい場合は以下のようにEventArgsを継承したクラスを作る。


    public class TestEventArgs : EventArgs
    {
        public string Hoge { get; set; }
    }

    public class EvenSample3
    {
        public event EventHandler<TestEventArgs>? TestEvent;

        public void Test()
        {
            var ea = new TestEventArgs();
            ea.Hoge = "TEST";
            if (TestEvent != null) TestEvent(this, ea);
        }
    }

    public class EventSampleTest(ITestOutputHelper output)
    {
        [Fact]
        public void c()
        {
            var a = new EvenSample3();
            a.TestEvent += (sender, e) =>
            {
                Assert.Equal(a, sender);
                Assert.Equal("TEST", e.Hoge);
            };
            a.Test();
        }
    }