델리게이트 예제
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | using System; delegate void Attack(int damage); namespace EX09_Delegate { class Weapon { protected int Damage { get; set; } } class AssultRifle : Weapon { public void Shoot(int damage) { this.Damage = damage; Console.WriteLine ("Shoot Ammo : {0}", Damage); } } class Knife : Weapon { public void Sweep(int damage) { this.Damage = damage; Console.WriteLine ("Sweep Knife : {0}", Damage); } } class Grenade : Weapon { public void Throw(int damage) { this.Damage = damage; Console.WriteLine ("Throw Grenade : {0}", Damage); } } class MainClass { public static void Shoot(int damage) { Console.WriteLine ("Pistol Shoot : {0}", damage); } public static void Main (string[] args) { Attack pistolAttack = new Attack (Shoot); pistolAttack(50); AssultRifle assultRifle = new AssultRifle (); Knife knife = new Knife (); Grenade grenade = new Grenade (); Attack[] playerAttack = { new Attack (assultRifle.Shoot), new Attack (knife.Sweep), new Attack (grenade.Throw) }; playerAttack[0](100); playerAttack[1](200); playerAttack[2](300); Console.WriteLine ("***** Chain Attack *****"); Attack chainAttack = Delegate.Combine (playerAttack [0], playerAttack [0], playerAttack [1], playerAttack [1], playerAttack [2]) as Attack; chainAttack (200); Console.WriteLine ("***** Chain Attack 2*****"); Attack chainAttack2 = new Attack(playerAttack[0]); chainAttack2 += new Attack(playerAttack[1]); chainAttack2 += new Attack(playerAttack[2]); chainAttack2 += new Attack(playerAttack[2]); chainAttack2 += new Attack(playerAttack[0]); chainAttack2 (300); Console.WriteLine ("***** Chain Attack 3*****"); Attack chainAttack3 = new Attack(playerAttack[1]) + new Attack(playerAttack[2]) + new Attack(playerAttack[0]) - new Attack(playerAttack[2]); chainAttack3 (400); } } } | cs |
델리게이트 익명 메소드 예제
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; delegate void Operation(int a, int b); namespace EX09_DelegateNoName { class MainClass { public static Operation calculate; public static void Main (string[] args) { calculate = delegate(int a, int b) { Console.WriteLine ("{0} + {1} = {2}", a, b, a + b); }; calculate (83, 34); calculate = delegate(int a, int b) { Console.WriteLine ("{0} - {1} = {2}", a, b, a - b); }; calculate (83, 34); calculate = delegate(int a, int b) { Console.WriteLine ("{0} * {1} = {2}", a, b, a * b); }; calculate (83, 34); calculate = delegate(int a, int b) { Console.WriteLine ("{0} / {1} = {2}", a, b, a / b); }; calculate (83, 34); } } } | cs |
Func 델리게이트 예제
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 36 37 38 39 40 41 42 | using System; namespace EX09_FuncDelegate { class MainClass { class Calculator { public static int Add(int a, int b) { return a + b; } public static int Subtract(int a, int b) { return a - b; } public static int Multiply(int a, int b) { return a * b; } public static int Divide(int a, int b) { return a / b; } } public static void Main (string[] args) { Func<int, int, int> calculate; calculate = Calculator.Add; Console.WriteLine ("{0} + {1} = {2}", 83, 34, calculate (83, 34)); calculate = Calculator.Subtract; Console.WriteLine ("{0} - {1} = {2}", 83, 34, calculate (83, 34)); calculate = Calculator.Multiply; Console.WriteLine ("{0} * {1} = {2}", 83, 34, calculate (83, 34)); calculate = Calculator.Divide; Console.WriteLine ("{0} / {1} = {2}", 83, 34, calculate (83, 34)); } } } | cs |
Action 델리게이트 예제
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 36 37 38 39 40 41 42 | using System; namespace EX09_ActionDelegate { class MainClass { class Calculator { public static void Add(int a, int b) { Console.WriteLine ("{0} + {1} = {2}", a, b, a + b); } public static void Subtract(int a, int b) { Console.WriteLine ("{0} - {1} = {2}", a, b, a - b); } public static void Multiply(int a, int b) { Console.WriteLine ("{0} * {1} = {2}", a, b, a * b); } public static void Divide(int a, int b) { Console.WriteLine ("{0} / {1} = {2}", a, b, a / b); } } public static void Main (string[] args) { Action<int, int> calculate; calculate = Calculator.Add; calculate (83, 34); calculate = Calculator.Subtract; calculate (83, 34); calculate = Calculator.Multiply; calculate (83, 34); calculate = Calculator.Divide; calculate (83, 34); } } } | cs |
이벤트 예제
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 | using System; namespace EX09_Event { delegate int Operator(int a, int b); class Calculator { public event Operator operatorCB; public void calculate (int a, int b, string op, Operator opCB) { operatorCB = opCB; Console.WriteLine("{0} {1} {2} = {3}", a, op, b, operatorCB (a, b)); } } class MainClass { public static void Main (string[] args) { Calculator calculator = new Calculator(); calculator.calculate (83, 34, "+", delegate(int a, int b) { return a + b; }); calculator.calculate (83, 34, "-", delegate(int a, int b) { return a - b; }); calculator.calculate (83, 34, "*", delegate(int a, int b) { return a * b; }); calculator.calculate (83, 34, "/", delegate(int a, int b) { return a / b; }); } } } | cs |
'Programming > C#' 카테고리의 다른 글
[C# 프로그래밍] 11장 람다식 (0) | 2016.01.11 |
---|---|
[C# 프로그래밍] 10장 LINQ (0) | 2016.01.11 |
[C# 프로그래밍] 8장 반복기와 양보문 (0) | 2016.01.11 |
[C# 프로그래밍] 7장 인터페이스와 추상클래스 (0) | 2016.01.11 |
[C# 프로그래밍] 6강 제네릭 프로그래밍 (0) | 2016.01.11 |