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.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
delegate void EventDelegate(int n); //デリゲートの宣言
class Program
{
static void Main(string[] args)
{
Class obj = new Class();
obj.myEvent += method; //イベントハンドラの登録
obj.calc();
Console.ReadLine();
}
static public void method(int n) //イベントハンドラの実装
{
if (n != 50)
{
Console.WriteLine(string.Format("iの値が{0}になりました", n));
}
else
{
Console.WriteLine("iの値が50になりましたので演算を終了します");
}
}
}
class Class
{
public event EventDelegate myEvent; //イベントの宣言
public void calc()
{
int i = 0;
while(i<=50)
{
if (i % 10 == 0)
{
myEvent(i);
}
i++;
}
}
}
} |