delegate 란 무엇일까? 어원 뜻대로는 "대표"라는 뜻이다.
그럼 이것을 왜 사용하는 걸까? 특별한 용도로 사용되기 위한 함수를 간략히 요약한 참조타입을 정의하는 것이라고 할 수 있어요..
그리고 하나의 함수에 대해서 정의를 되며, 그리고 런타임싱 생성된답니다.
우선 delegate 의 형태는 어떻게 생겼을까요?
public delegate int MyDelegate(int nKorea, string nShim);
- public : 접근지정자
- delegate : delegate 예약어
- int : 리턴형
- MyDelegate : delegate 함수이름임다
- (int nKorea, string nShim) : 사용하게 될 인자임다.
예제를 볼까요?
예제)
using System;
public delegate void DelegateTest(int nKorea); // delegate 함수선언
class DelegateClass {
public void delegateMethod (int nKorea) { //delegate 호출함수 구현
Console.WriteLine("who are you : {0}", nKorea); // 출력문...
}
}
class MainClass {
static public void Main() {
DelegateClass shimpark = new DelegateClass(); //객체 생성
DelegateTest methodDelegate = new DelegateTest (shimpark.delegateMethod);//delegate 함수만듬
methodDelegate(13);
}
}
출력 : who are you : 13
- 여기서요.~~~ delegate 함수와 delegate 에 사용될 함수의 인자가 같아야 된다는 사실을 잊어서는 안되요 ^^;
* 음...우린 여기서 DelegateClass의 객체를 생성했는데요..참 불편하지 않으세요? 그래서 객체 생성없이 사용할 수 있는데요. static 으로 정의 해주면 쉽게 해결할 수 있답니다.
using System;
public delegate void DelegateTest(int nKorea); // delegate 함수선언
class DelegateClass {
static public void delegateMethod (int nKorea) { //delegate 호출함수 구현 및 static 이용으로 객체 생성 하지 않음
Console.WriteLine("who are you : {0}", nKorea); // 출력문...
}
}
class MainClass {
static public void Main() {
DelegateTest methodDelegate = new DelegateTest (DelegateClass.delegateMethod); //delegate 함수만들었고 , 직접참조함
methodDelegate(13);
}
}
좀 간단하게 되었죠? 그러나 더 욕심히 생기면 methodDelegate 객체조차도 생성하지 않게 하고자 한다면...어떻게 할까요? ^^;
우리 property 를 사용해서 한번 써보죠 ^^;
using System;
public delegate void DelegateTest(int nKorea); // delegate 함수선언
class DelegateClass {
static public void delegateMethod (int nKorea) { //delegate 호출함수 구현 및 static 이용으로 객체 생성 하지 않음
Console.WriteLine("who are you : {0}", nKorea); // 출력문...
}
public static DelegateTest korea { // property 문을 이용한것임
get {
return (new DelegateTest(delegateMethod))
}
}
class MainClass {
static public void Main() {
DelegateTest methodDelegate = DelegateClass.korea;//property를 이용해서 delegate 객체를 받아냅니다.
methodDelegate(13);
}
}
간단히 소개를 했는데 어떠 했는지 모르겠네요..
궁금한거 아래 시샵질문 게시판 잇죠 ?? 거기에다 질문해주세요..
제 가 힘 다는데 까지 알려 드릴께요..그럼..언능..주무세요.
저도 피곤해서 자야 겠네요..그럼.수고하세요..^^; 재운이가.
그럼 이것을 왜 사용하는 걸까? 특별한 용도로 사용되기 위한 함수를 간략히 요약한 참조타입을 정의하는 것이라고 할 수 있어요..
그리고 하나의 함수에 대해서 정의를 되며, 그리고 런타임싱 생성된답니다.
우선 delegate 의 형태는 어떻게 생겼을까요?
public delegate int MyDelegate(int nKorea, string nShim);
- public : 접근지정자
- delegate : delegate 예약어
- int : 리턴형
- MyDelegate : delegate 함수이름임다
- (int nKorea, string nShim) : 사용하게 될 인자임다.
예제를 볼까요?
예제)
using System;
public delegate void DelegateTest(int nKorea); // delegate 함수선언
class DelegateClass {
public void delegateMethod (int nKorea) { //delegate 호출함수 구현
Console.WriteLine("who are you : {0}", nKorea); // 출력문...
}
}
class MainClass {
static public void Main() {
DelegateClass shimpark = new DelegateClass(); //객체 생성
DelegateTest methodDelegate = new DelegateTest (shimpark.delegateMethod);//delegate 함수만듬
methodDelegate(13);
}
}
출력 : who are you : 13
- 여기서요.~~~ delegate 함수와 delegate 에 사용될 함수의 인자가 같아야 된다는 사실을 잊어서는 안되요 ^^;
* 음...우린 여기서 DelegateClass의 객체를 생성했는데요..참 불편하지 않으세요? 그래서 객체 생성없이 사용할 수 있는데요. static 으로 정의 해주면 쉽게 해결할 수 있답니다.
using System;
public delegate void DelegateTest(int nKorea); // delegate 함수선언
class DelegateClass {
static public void delegateMethod (int nKorea) { //delegate 호출함수 구현 및 static 이용으로 객체 생성 하지 않음
Console.WriteLine("who are you : {0}", nKorea); // 출력문...
}
}
class MainClass {
static public void Main() {
DelegateTest methodDelegate = new DelegateTest (DelegateClass.delegateMethod); //delegate 함수만들었고 , 직접참조함
methodDelegate(13);
}
}
좀 간단하게 되었죠? 그러나 더 욕심히 생기면 methodDelegate 객체조차도 생성하지 않게 하고자 한다면...어떻게 할까요? ^^;
우리 property 를 사용해서 한번 써보죠 ^^;
using System;
public delegate void DelegateTest(int nKorea); // delegate 함수선언
class DelegateClass {
static public void delegateMethod (int nKorea) { //delegate 호출함수 구현 및 static 이용으로 객체 생성 하지 않음
Console.WriteLine("who are you : {0}", nKorea); // 출력문...
}
public static DelegateTest korea { // property 문을 이용한것임
get {
return (new DelegateTest(delegateMethod))
}
}
class MainClass {
static public void Main() {
DelegateTest methodDelegate = DelegateClass.korea;//property를 이용해서 delegate 객체를 받아냅니다.
methodDelegate(13);
}
}
간단히 소개를 했는데 어떠 했는지 모르겠네요..
궁금한거 아래 시샵질문 게시판 잇죠 ?? 거기에다 질문해주세요..
제 가 힘 다는데 까지 알려 드릴께요..그럼..언능..주무세요.
저도 피곤해서 자야 겠네요..그럼.수고하세요..^^; 재운이가.
다음검색