C# 델리게이트+이벤트 관련 문의 드립니다
조회수 877회
namespace ConsoleApplication6
{
delegate void EventHandler(string message);
class MyNotifier
{
public event EventHandler SomethingHappened;
public void DoSomething(int number)
{
int temp = number % 10;
if(temp !=0 && temp % 3 ==0)
{
SomethingHappened(String.Format("{0} : 짝", number));
}
}
}
class MainApp
{
static public void MyHandler(string message)
{
Console.WriteLine(message);
}
static void Main(string[] args)
{
MyNotifier notifier = new MyNotifier();
notifier.SomethingHappened += new EventHandler(MyHandler);
for(int i=1;i<30;i++)
{
notifier.DoSomething(i);
}
}
}
}
위가 책에서 델리게이트+이벤트를 설명한 부분인데요 왜 이렇게 어렵게 델리게이트 만들고 이벤트 연결해서 사용하는지는 모르겠습니다
그냥
namespace ConsoleApplication6
{
class MyNotifier
{
public void DoSomething(int number)
{
int temp = number % 10;
if(temp !=0 && temp % 3 ==0)
{
Console.WriteLine(String.Format("{0} : 짝", number));
}
}
}
class MainApp
{
static public void MyHandler(string message)
{
Console.WriteLine(message);
}
static void Main(string[] args)
{
MyNotifier notifier = new MyNotifier();
for(int i=1;i<30;i++)
{
notifier.DoSomething(i);
}
}
}
}
이렇게 하는게 훨씬 낫지 않나 싶어서요 초보라 여러번 읽어보고 델리게이트 관련 찾아봐도 델리게이트는 약간 이해가 가는데 이벤트를 저렇게 처리하는건 도무지 이해가 안되네요
혹시 조금만 쉽게 설명 해주실수 있을까요?
감사합니다^
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력