오늘의 C# 공부 : 델리게이트

///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////델리게이트
////////////////////////////////////////////////////////////////////////////////////////////////////////////

//using System;

//namespace Delegate
//{
//    delegate int MyDelegate ( int a, int b);

//    class Calcurator
//    {
//        public int Plus(int a, int b)
//        {
//            return (a + b);
//        }

//        public static int Minus(int a, int b)
//        {
//            return (a - b);
//        }
//    }

//    class jpcorp
//    {
//        static void Main(string[] args)
//        {
//            Calcurator Calc = new Calcurator();
//            MyDelegate Callback;

//            Callback = new MyDelegate(Calc.Plus);
//            Console.WriteLine(Callback(3, 4));

//            Callback = new MyDelegate(Calcurator.Minus);
//            Console.WriteLine(Callback(7, 5));
//        }
//    }
//}

//// 델리게이트를 이용한 정렬 프로그램

//using System;

//namespace UsingCallback
//{
//    delegate int Compare(int a, int b);

//    class jpcorp
//    {
//        static int AscendCompare(int a, int b)
//        {
//            if (a > b)
//                return 1;
//            else if (a == b)
//                return 0;
//            else
//                return -1;
//        }

//        static int DescendCompare(int a, int b)
//        {
//            if (a < b)
//                return 1;
//            else if (a == b)
//                return 0;
//            else
//                return -1;
//        }

//        static void BubbleSort(int[] DataSet, Compare Comparer)
//        {
//            int i = 0;
//            int j = 0;
//            int temp = 0;

//            for (i = 0; i < DataSet.Length - 1; i++)
//            {
//                for (j = 0; j < DataSet.Length - (i + 1); j++)
//                {
//                    if (Comparer (DataSet[j],DataSet[j+1]) >0)
//                    {
//                        temp = DataSet[j + 1];
//                        DataSet[j + 1] = DataSet[j];
//                        DataSet[j] = temp;
//                    }
//                }
//            }
//        }

//        static void Main(string[] args)
//        {
//            int[] array = { 3, 7, 4, 2, 10 };

//            Console.WriteLine(“소팅 오름차순….”);
//            BubbleSort(array, new Compare(AscendCompare));

//            for (int i = 0; i < array.Length; i++)
//                Console.Write("{0} “, array[i]);

//            int[] array2 = { 7, 2, 8, 10, 11 };

//            Console.WriteLine("\n소팅 내림차순”);
//            BubbleSort(array2, new Compare(DescendCompare));

//            for (int i = 0; i < array2.Length; i++)
//                Console.Write("{0} “, array2[i]);

//            Console.WriteLine();

//        }
//    }
//}

////일반화 델리게이트

//using System;

//namespace GenericDelegate
//{
//    delegate int Compare(T a, T b);

//    class jpcorp
//    {
//        static int AscendCompare(T a, T b) where T : IComparable
//        {
//            return a.CompareTo(b);
//        }

//        static int DescendCompare(T a, T b) where T : IComparable
//        {
//            return a.CompareTo(b) * -1;
//        }

//        static void BubbleSort(T[] DataSet, Compare Comparer)
//        {
//            int i = 0;
//            int j = 0;
//            T temp;

//            for (i = 0; i < DataSet.Length - 1; i++)
//            {
//                for (j = 0; j < DataSet.Length - (i + 1); j++)
//                {
//                    if (Comparer(DataSet[j], DataSet[j + 1]) > 0)
//                    {
//                        temp = DataSet[j + 1];
//                        DataSet[j + 1] = DataSet[j];
//                        DataSet[j] = temp;
//                    }
//                }
//            }
//        }

//        static void Main(string[] args)
//        {
//            int[] array = { 3, 7, 4, 2, 10 };
//            Console.WriteLine(“오름차순 정리…”);
//            BubbleSort(array, new Compare(AscendCompare));

//            for (int i = 0; i < array.Length; i++)
//                Console.Write("{0} “, array[i]);

//            string[] array2 = { “abd”, “def”, “ghi”, “jkl”, “mno” };

//            Console.WriteLine("\n내림차순….”);

//            BubbleSort(array2, new Compare(DescendCompare));

//            for ( int i =0; i<array2.Length; i++)
//                Console.Write("{0} “,array2[i]);

//            Console.WriteLine();

//        }
//    }
//}

////델리게이트 체인
//using System;

//namespace DelegateChains
//{
//    delegate void Notify ( string meassage);

//    class Notifier
//    {
//        public Notify EventOccured;
//    }

//    class EventListener
//    {
//        private string name;
//        public EventListener(string name)
//        {
//            this.name = name;
//        }

//        public void SomethingHappend(string message)
//        {
//            Console.WriteLine("{0}.SomethingHappened : {1}”, name, message);
//        }
//    }

//    class jpcorp
//    {
//        static void Main(string[] args)
//        {
//            Notifier notifier = new Notifier();
//            EventListener listener1 = new EventListener(“Listener1”);
//            EventListener listener2 = new EventListener(“Listener2”);
//            EventListener listener3 = new EventListener(“Listener3”);

//            notifier.EventOccured += listener1.SomethingHappend;
//            notifier.EventOccured += listener2.SomethingHappend;
//            notifier.EventOccured += listener3.SomethingHappend;

//            notifier.EventOccured(“You’ve got mail”);
//            Console.WriteLine();

//            notifier.EventOccured -= listener2.SomethingHappend;
//            notifier.EventOccured(“Download complete.”);

//            Console.WriteLine();

//            notifier.EventOccured = new Notify(listener2.SomethingHappend) + new Notify(listener3.SomethingHappend);
//            notifier.EventOccured(“Nuclear launch derected”);

//            Console.WriteLine();

//            Notify notify1 = new Notify(listener1.SomethingHappend);
//            Notify notify2 = new Notify(listener2.SomethingHappend);

//            notifier.EventOccured = (Notify)Delegate.Combine(notify1, notify2);
//            notifier.EventOccured(“Fire!”);

//            Console.WriteLine();

//            notifier.EventOccured = (Notify)Delegate.Remove(notifier.EventOccured, notify2);
//            notifier.EventOccured(“RPG”);

//        }
//    }
//}

////익명 메소드
//using System;
//namespace AnonymousMethod
//{
//    delegate int Compare ( int a, int b);

//    class jpcorp
//    {
//        static void BubbleSort(int[] DataSet, Compare Comparer)
//        {
//            int i = 0;
//            int j = 0;
//            int temp = 0;

//            for ( i =0; i < DataSet.Length-1 ; i ++)
//            {
//                for (j = 0; j<DataSet.Length - (i+1);j++)
//                {
//                    if (Comparer(DataSet[j] , DataSet[j+1] ) >0)
//                    {
//                        temp = DataSet[j+1];
//                        DataSet[j+1] = DataSet[j];
//                        DataSet[j] = temp;
//                    }
//                }
//            }
//        }

//        static void Main(string[] args)
//        {
//            int[] array = { 3, 7, 4, 2, 10 };
//            Console.WriteLine(“오름차순 소팅…”);
//            BubbleSort(array, delegate(int a, int b)
//                {
//                    if (a > b)
//                        return 1;
//                    else if (a == b)
//                        return 0;
//                    else
//                        return -1;
//                 });

//            for ( int i = 0 ; i <array.Length ; i++)
//                Console.Write("{0} “,array[1]);

//            int[] array2 = {7,2,8,10,11};
//            Console.WriteLine ("\n 내림차순 소팅…..”);
//            BubbleSort(array2, delegate( int a, int b)
//                {
//                    if (a > b)
//                        return 1;
//                    else if (a == b)
//                        return 0;
//                    else
//                        return -1;

//                });

//            for ( int i= 0; i <array2.Length; i++)
//                Console.Write("{0} “,array2[i]);

//            Console.WriteLine();
//        }
//    }
//}

Hugo로 만듦
JimmyStack 테마 사용 중