2016년 4월 30일 토요일

[C#] Callback Func



예제 1

  1. namespace Cshop_v6._0
  2. {
  3. delegate int GetResultDelegate();
  4.  
  5. class Target
  6. {
  7. public void Do(GetResultDelegate getResult)
  8. {
  9. Console.WriteLine(getResult()); // 콜백 메서드 호출
  10. }
  11. }
  12.  
  13. class Source
  14. {
  15. public int GetResult() // 콜백 용도로 전달된 메서드
  16. {
  17. return 10;
  18. }
  19.  
  20. public void Test()
  21. {
  22. Target target = new Target();
  23. target.Do(new GetResultDelegate(this.GetResult));
  24. }
  25. }
  26. }

예제 2

  1. namespace Cshop_v6._0
  2. {
  3. delegate int GetResultDelegate(int x, int y);
  4.  
  5. class Target
  6. {
  7. public void Do(GetResultDelegate getResult, int x, int y)
  8. {
  9. Console.WriteLine(getResult(x, y));
  10. }
  11. }
  12.  
  13. class Source
  14. {
  15. public static int GetResult(int x, int y)
  16. {
  17. return x * y;
  18. }
  19.  
  20. public static void Main()
  21. {
  22. Target target = new Target();
  23. GetResultDelegate gd = GetResult;
  24. target.Do(gd, 5, 10);
  25. }
  26. }
  27. }

[C#] Delegate



접근제한자 delegate 대상_메서드_반환타입 식별자(… … 대상_메서드_매개변수_목록 … …);

예제 1

  1. namespace Cshop_v6._0
  2. {
  3. public class Mathematics
  4. {
  5. delegate int CalcDelegate(int x, int y);
  6.  
  7. static int Add(int x, int y) { return x + y; }
  8. static int Subtract(int x, int y) { return x - y; }
  9. static int Multiply(int x, int y) { return x * y; }
  10. static int Devide(int x, int y) { return x / y; }
  11.  
  12. CalcDelegate[] methods;
  13.  
  14. public Mathematics()
  15. {
  16. methods = new CalcDelegate[] { Mathematics.Add, Mathematics.Subtract, Mathematics.Multiply, Mathematics.Devide };
  17. }
  18.  
  19. public void Calculate(char opCode, int operand1, int operand2)
  20. {
  21. switch (opCode)
  22. {
  23. case '+': Console.WriteLine("+: " + methods[0](operand1, operand2)); break;
  24. case '-': Console.WriteLine("-: " + methods[1](operand1, operand2)); break;
  25. case '*': Console.WriteLine("*: " + methods[2](operand1, operand2)); break;
  26. case '/': Console.WriteLine("/: " + methods[3](operand1, operand2)); break;
  27. }
  28. }
  29. }
  30.  
  31. class Program
  32. {
  33. delegate void WorkDelegate(char arg1, int arg2, int arg3);
  34.  
  35. static void Main()
  36. {
  37. Mathematics math = new Mathematics();
  38. WorkDelegate work = math.Calculate;
  39.  
  40. work('+', 10, 5);
  41. work('-', 10, 5);
  42. work('*', 10, 5);
  43. work('/', 10, 5);
  44. }
  45. }
  46. }

예제 2

  1. namespace Cshop_v6._0
  2. {
  3. class Program
  4. {
  5. delegate void CalcDelegate(int x, int y);
  6.  
  7. static void Add(int x, int y) { Console.WriteLine(x + y); }
  8. static void Subtract(int x, int y) { Console.WriteLine(x - y); }
  9. static void Multiply(int x, int y) { Console.WriteLine(x * y); }
  10. static void Divide(int x, int y) { Console.WriteLine(x / y); }
  11.  
  12. static void Main()
  13. {
  14. CalcDelegate calc = Add;
  15. calc += Subtract;
  16. calc += Multiply;
  17. calc += Divide;
  18.  
  19. calc(10, 5);
  20.  
  21. // 출력결과
  22. /*
  23. 15
  24. 5
  25. 50
  26. 2
  27. */
  28.  
  29. Console.WriteLine("=================================================================");
  30.  
  31. calc -= Divide;
  32.  
  33. calc(10, 5);
  34.  
  35.  
  36. // 출력결과
  37. /*
  38. 15
  39. 5
  40. 50
  41. */
  42. }
  43. }
  44. }