티스토리 뷰
Delegate & Event 예제
간단한 Delegate & Event 예제 입니다.
아래에서 주의 할 점은 public event 를 부를 수 있는 것은 그 event 가 속해있는 class의 method 뿐이라는 것입니다.
Anonymous Method 의 사용법도 눈여겨보시기 바랍니다.
Anonymous Method 를 사용할 때, close brace " } " 다음에 세미콜론 " ; " 을 붙여야합니다.
간단한 Delegate & Event 예제 입니다.
아래에서 주의 할 점은 public event 를 부를 수 있는 것은 그 event 가 속해있는 class의 method 뿐이라는 것입니다.
Anonymous Method 의 사용법도 눈여겨보시기 바랍니다.
Anonymous Method 를 사용할 때, close brace " } " 다음에 세미콜론 " ; " 을 붙여야합니다.
using System;
namespace DelegateSample
{
class Program
{
static void Main(string[] args)
{
Human Tom = new Human();
Tom.ActionDelegate += new Action(Tom.Speak);
Tom.ActionDelegate += Tom.Shout;
// Anonymous Method
Tom.ActionDelegate += delegate(string words)
{
Console.WriteLine("Anonymous:" + words);
};
Tom.ActionDelegate("Direct Called Delegate");
Tom.RaiseActionDelegate("Delegate Raised by Method");
Tom.ActionEvent += new Action(Tom.Speak);
Tom.ActionEvent += Tom.Shout;
// Anonymous Method
Tom.ActionEvent += delegate(string words)
{
Console.WriteLine("Anonymous:" + words);
};
// A public event can only be raised
// by methods within the class that defines it.
// Tom.ActionEvent("Direct Called Event"); // Compile Error.
Tom.RaiseActionEvent("Event Raised by Method");
}
delegate void Action(string words);
class Human
{
public void Speak(string words)
{
Console.WriteLine("Speak:" + words);
}
public void Shout(string words)
{
Console.WriteLine("Shout:" + words);
}
public void RaiseActionDelegate(string words)
{
if (this.ActionDelegate != null)
{
this.ActionDelegate(words);
}
}
public void RaiseActionEvent(string words)
{
if (this.ActionEvent != null)
{
this.ActionEvent(words);
}
}
public Action ActionDelegate;
public event Action ActionEvent;
}
}
}
'Code > C#' 카테고리의 다른 글
[C#] Multidimensional Array 선언하는 방법 (0) | 2007.11.29 |
---|---|
[C#] Array 선언하는 방법 (0) | 2007.11.29 |
[C#] Random 샘플 코드 (0) | 2007.11.27 |
[C#] Event - Anonymous method 간단한 예제 코드 (0) | 2007.11.26 |
[C#] Delegate - Anonymous method 간단한 예제 코드 (0) | 2007.11.26 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ScreenHunter
- iText
- download.com
- c#
- windows
- Phalanger
- DotNetMagic
- AdSense감추기
- jre
- Automation
- Rollback Rx
- 애드센스감추기
- READYSTATE_COMPLETE
- Sample Code
- .net framework
- 유틸리티
- Regular Expressions
- iTextSharp
- Microsoft
- AdSense숨기기
- tagREADYSTATE
- 스크린캡쳐
- WinAutomation
- java
- Service pack
- AxWebBrowser
- 애드센스숨기기
- registry
- autohotkey
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함