델리게이트 예제

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 (8334);
 
            calculate = delegate(int a, int b) {
                Console.WriteLine ("{0} - {1} = {2}", a, b, a - b);
            };
            calculate (8334);
 
            calculate = delegate(int a, int b) {
                Console.WriteLine ("{0} * {1} = {2}", a, b, a * b);
            };
            calculate (8334);
 
            calculate = delegate(int a, int b) {
                Console.WriteLine ("{0} / {1} = {2}", a, b, a / b);
            };
            calculate (8334);
        }    
    }
}
 
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<intintint> calculate;
 
            calculate = Calculator.Add;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            calculate = Calculator.Subtract;
            Console.WriteLine ("{0} - {1} = {2}"8334, calculate (8334));
 
            calculate = Calculator.Multiply;
            Console.WriteLine ("{0} * {1} = {2}"8334, calculate (8334));
 
            calculate = Calculator.Divide;
            Console.WriteLine ("{0} / {1} = {2}"8334, calculate (8334));
        }
    }
}
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<intint> calculate;
            
            calculate = Calculator.Add;
            calculate (8334);
            
            calculate = Calculator.Subtract;
            calculate (8334);
            
            calculate = Calculator.Multiply;
            calculate (8334);
            
            calculate = Calculator.Divide;
            calculate (8334);
        }
    }
}
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 (8334"+"delegate(int a, int b) { return a + b; });
            calculator.calculate (8334"-"delegate(int a, int b) { return a - b; });
            calculator.calculate (8334"*"delegate(int a, int b) { return a * b; });
            calculator.calculate (8334"/"delegate(int a, int b) { return a / b; });
        }
    }
}
 
cs


반복기와 양보문 예제 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
 
namespace EX08_Yield_Enumerator_1
{
    class MainClass
    {
        public static System.Collections.Generic.IEnumerable<int> AddNumber(int n) {
            int result = 0;
            for (int i = 0; i <= n; i++)
            {
                result += i;
                yield return result;
            }
        }
 
        public static void Main (string[] args) {
            foreach (int i in AddNumber(10)) {
                Console.WriteLine("{0} ", i);
            }
        }
    }
}
cs

반복기와 양보문 예제 2

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
using System;
 
namespace EX08_Yield_Enumerator_2
{
    public class Enemy 
    {
        public String Name { get; set; }
        public int HP { get; set; }
        public int LEVEL { get; set; }
    }
    
    public class EnemyList 
    {            
        public System.Collections.Generic.IEnumerable<Enemy> Next {
            get {
                yield return new Enemy { Name = "슬라임", LEVEL = 1, HP = 50 };
                yield return new Enemy { Name = "좀비", LEVEL = 2, HP = 100 };
                yield return new Enemy { Name = "해골", LEVEL = 2, HP = 150 };
                yield return new Enemy { Name = "구울", LEVEL = 3, HP = 220 };
            }
        }        
    }
 
    public class MainClass
    {
        public static void PrintEnemyList() {
            var enemys = new EnemyList();
            foreach (Enemy enemy in enemys.Next) {
                Console.WriteLine("LV {0} {1} 의 HP는 {2}",    enemy.LEVEL,
                                                              enemy.Name,
                                                              enemy.HP);
            }
        }
        
        static void Main(string[] args) {
            PrintEnemyList();
        }
    }
}
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
36
37
38
39
using System;
 
namespace InterfaceEx
{
    interface IA
    {
        void MustBeImplement(string text);
    }
 
    interface IB
    {
        string Name { get; set; }
    }
    
    class ClassB : IA, IB
    {
        public string Name { get; set; }
        public void MustBeImplement(string name)
        {
            this.Name = name;
            Console.WriteLine("{0} implement this method!!"this.Name);
        }
    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            ClassB b = new ClassB();
            b.MustBeImplement("B");
            
            //IA iaa = new IA();
            
            IA ia = new ClassB();
            ia.MustBeImplement("IA");
            
            Console.ReadKey();
        }
    }
}
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
36
37
38
39
40
41
42
43
44
45
using System;
 
namespace AbstractClassEx
{
    abstract class ClassA
    {
        private string txtA;
        protected string txtB;
 
        public void WriteTextToConsole(string text)
        {
            this.txtA = text;
            Console.WriteLine(this.txtA);
        }
        
        public abstract void MustBeImplemented(string text);
    }
    
    class classB : ClassA
    {
        public override void MustBeImplemented(string text)
        {
            this.txtB = text;
            Console.WriteLine("{0} implement this method"this.txtB);
        }
    }
 
    class MainClass
    {
        public static void Main (string[] args)
        {
            classB b = new classB();
            b.WriteTextToConsole("B");
            b.MustBeImplemented("B");
            
            //ClassA aa = new ClassA();
            
            ClassA a = new classB();
            a.WriteTextToConsole("A");
            a.MustBeImplemented("A");
            
            Console.ReadKey();
        }
    }
}
cs


+ Recent posts