・呼出3種類
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var t1 = new TestClass1(); t1.TestEvent += (s, e) => { Console.WriteLine("TestEvent"); Console.ReadKey(); }; t1.OnTestMethod1(); t1.OnTestMethod2(); t1.OnTestMethod3(); } } class TestClass1 { public event EventHandler TestEvent; // 呼出3種類 public void OnTestMethod1() { if (TestEvent != null) { TestEvent(this, new EventArgs()); } } public void OnTestMethod2() { TestEvent?.Invoke(this, new EventArgs()); } public void OnTestMethod3() => TestEvent?.Invoke(this, new EventArgs()); } } |
・Eventフィールドを省略しない場合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var t1 = new TestClass1(); t1.TestEvent += (s, e) => { Console.WriteLine("Test"); Console.ReadKey(); }; t1.OnTestMethod(); } } class TestClass1 { private EventHandler testEvent; public event EventHandler TestEvent { add { testEvent = testEvent + value; } remove { testEvent = testEvent - value; } } public void OnTestMethod() => testEvent?.Invoke(this, new EventArgs()); } } |
・独自Event定義
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var t1 = new TestClass1(); t1.TestEvent += (s, e) => { Console.WriteLine(e.Id.ToString() + ":" + e.Msg); Console.ReadKey(); }; t1.OnTestMethod(1,"hello"); t1.OnTestMethod(2,"bye"); } } // TestEventArgsを引数にとる独自Eventの定義 delegate void TestEventHandler(object sender, TestEventArgs e); class TestClass1 { public event TestEventHandler TestEvent; public void OnTestMethod(int id, string msg) => TestEvent?.Invoke(this, new TestEventArgs(id, msg)); } // 独自EventArgsの定義 class TestEventArgs : EventArgs { public int Id; public string Msg; public TestEventArgs(int i, string m) { this.Id = i; this.Msg = m; } } } |
・async + await定義
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
using System; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var t1 = new TestClass1(); t1.TestEvent += (s, e) => { return Task.Run(() => { Console.WriteLine(e.Id.ToString() + ":" + e.Msg); }); }; t1.OnTestMethod(1,"hello"); Console.ReadKey(); } } // TestEventArgsを引数にとる独自Eventの定義 // Taskを返すEvent delegate Task TestEventHandler(object sender, TestEventArgs e); class TestClass1 { public event TestEventHandler TestEvent; // async + await定義 public async void OnTestMethod(int id, string msg) => await TestEvent?.Invoke(this, new TestEventArgs(id, msg)); } // 独自EventArgsの定義 class TestEventArgs : EventArgs { public int Id; public string Msg; public TestEventArgs(int i, string m) { this.Id = i; this.Msg = m; } } } |