Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

행복한 개구리

C# 21.03.23.복습 본문

C#/복습

C# 21.03.23.복습

HappyFrog 2021. 3. 23. 23:13

델리게이트1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    class Program
    {
        static void Main(string[] frgs)
        {
            new Program().Test();	//App클래스에선 Test에 에러뜸
        }

        // 델리게이트 정의
        delegate int MyDelegate(string s);

        void Test()
        {
            //델리게이트 객체 생성
            MyDelegate m = new MyDelegate(StringToInt);

            //델리게이트 객체를 메서드로 전달
            Run(m);
        }

        // 델리게이트 대상이 되는 어떤 메서드
        int StringToInt(string s)
        {
            return int.Parse(s);
        }

        // 델리게이트를 전달 받는 메서드
        void Run(MyDelegate m)
        {
            // 델리게이트로부터 메서드 실행
            int i = m("123");

            Console.WriteLine(i);
        }
    }
}

델리게이트 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    public class App
    {
        private delegate void RunDelegate(int x);

        private void Runthis(int x)
        {
            Console.WriteLine("{0}", x);
        }

        private void Runthat(int x)
        {
            Console.WriteLine("0x{0:x}", x);
        }

        public void Perform()
        {
            RunDelegate run = new RunDelegate(Runthis);
            RunDelegate run2 = new RunDelegate(Runthat);
            run(452);
            run2(749);
        }

        public App()
        {
            Console.WriteLine("App");
            Perform();
        }

        
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    public class App
    {
        public delegate int CompareDelegate(int x, int y);

        public void Sort(int[] arr, CompareDelegate comp)
        {
            if (arr.Length < 2) return;
            Console.WriteLine("함수 Prototype:" + comp.Method);	//오..신기방기 꿀팁하나 배워갑니다.

            int ret;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                for (int j = i + 1; j < arr.Length; j++)
                {
                    ret = comp(arr[i], arr[j]);					//delegate가 이런식으로도 인스턴스를 받는구나
                    if (ret == -1)
                    {
                        int temp = arr[j];
                        arr[j] = arr[i];
                        arr[i] = temp;
                    }
                }
            }
            Display(arr);
        }

        public void Display(int[] arr)
        {
            foreach (int i in arr) Console.WriteLine(i + " ");
            Console.WriteLine();
        }

        public int AscendingCompare(int x, int y)
        {
            if (x == y) return 0;

            return (y - x) > 0 ? 1 : -1;

        }

        public int DescendingCompare(int x, int y)
        {
            if (x == y) return 0;
            return (x - y) > 0 ? 1 : -1;
        }
        public App()
        {
            int[] arr = { 1, 2, 3, 4, 5 };
            CompareDelegate compDelegate = AscendingCompare;
            Sort(arr, compDelegate);

            compDelegate = DescendingCompare;
            Sort(arr, compDelegate);
        }
    }
}

 

 

 

델리게이트 3

 

 

'C# > 복습' 카테고리의 다른 글

C# 21.04.05.복습  (0) 2021.04.05
C#21.03.29.복습  (0) 2021.03.29
C#21.03.26~28.복습  (0) 2021.03.26
C# 21.03.25.복습  (0) 2021.03.25