0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

イベント メモ

Posted at

EventHandlerとEventArgsについて調べたのでそれのメモ。

using System;

public class EventTest
{
    // イベントに何をするかを登録する。
    private void Start()
    {
        var sample = new EventSample();
        sample.HogeHappend += DoSomething;
    }

    // 何をするかの中身
    private void DoSomething(object sender, HogeEventArgs e)
    {
        Console.Write(e.Text);
        Console.Write(e.Num);
    }
}

// イベントを着火する側のクラス。
public class EventSample
{
    public void DoSomething()
    {
        // 引数を用意して着火();
        var e = new HogeEventArgs();
        OnEventHappend(e);
    }

    private void OnEventHappend(HogeEventArgs e)
    {
        // もしイベントがあれば実行。
        HogeHappend?.Invoke(this, e);
    }

    public event EventHandler<HogeEventArgs> HogeHappend;
}

// イベントの引数に持たせる情報。
public class HogeEventArgs : EventArgs
{
    public string Text;
    public int Num;
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?