http://ngcbbs.tistory.com/15
단하게 C/C++ 을 알고 있는 사용자에게 설명하면 함수 포인터 같은녀석! 이라고 이야기 할 수 있겠다.
다만 사용 방법이나 다중 위임의 형태를 C/C++ 에서 구현하기 위해서는 잡다한 코드가 더 추가되어야 하지만 정말 간편하게 다중 위임으로 처리가 가능하다.
delegate 키워드를 사용.
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 | using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ToolboxTestApp1 { class Program { // 위임이 무엇인고?? public delegate int Printer( string msg); // 함수 포인터 처럼 함수 바디는 필요 없다. public static int Printer_My1( string msg) { System.Console.WriteLine( "Printer_My1 = " + msg); return 1; } public static int Printer_My2( string msg) { System.Console.WriteLine( "Printer_My2 = " + msg); return 2; } static void Main( string [] args) { Printer _printer = new Printer(Printer_My1); _printer += new Printer(Printer_My2); // += 연산자를 통해서 다중 위임 처리. _printer( "한가한가~" ); // _printer 에 위임된 프린터가 동작한다. } } }
|
반응형
'프로그래밍(Programming) > C#' 카테고리의 다른 글
delegate 와 event 차이 , 이벤트 인스턴스 저장 (0) | 2015.09.01 |
---|---|
delegate (0) | 2015.08.31 |
[c#]class 앞에 붙는 public 이란? (0) | 2015.08.28 |
[C#]정적 생성자(static constructor) (0) | 2015.08.28 |
생성자 에서 virtual 함수 호출 (0) | 2015.08.27 |