πŸ’» Programming Language/C#

36. 읡λͺ…λ©”μ†Œλ“œ

S.Honey 2022. 4. 8. 09:13
  • 읡λͺ…λ©”μ†Œλ“œ(Anonymous Method) : 이름이 μ—†λŠ” λ©”μ†Œλ“œ

μ„ μ–Έν˜•μ‹

델리게이트 μΈμŠ€ν„΄μŠ€λͺ… = delegate(λ§€κ°œλ³€μˆ˜)
                    {
                        μ‹€ν–‰μ½”λ“œ ... 
                    }

μ‚¬μš© 예제 μ½”λ“œ

using System.Collections;

namespace AnonymousMethodEx
{
    delegate int CalDelegate(int x, int y);
    class Program
    {
        static void Main(string[] args)
        {
            CalDelegate cd;
            cd = delegate (int x, int y)
                {
                    return x * y;
                }; // => 읡λͺ…λ©”μ†Œλ“œ ν˜•νƒœ (이름이 μ—†μŒ)
                    // 단 λŒ€λ¦¬μžμ˜ ν˜•μ‹κ³Ό 같은 ν˜•μ‹μ„ μ¨μ•Όν•œλ‹€.
                     // κ³ λ €ν•΄μ•Ό ν•  ν˜•μ‹μ€ λ°˜ν™˜νƒ€μž…, μΈμžνƒ€μž…, μΈμžμˆ˜κ°€ μžˆλ‹€.

            Console.WriteLine($"{cd(10, 20)}");
        }
    }
}
Output

200
  • delegateλŠ” μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€ 수 μžˆλŠ” 클래슀처럼 λ™μž‘μ„ ν•˜κΈ° λ•Œλ¬Έμ— 클래슀 λ°–μœΌλ‘œ λΉΌμ„œ μž‘μ„±ν•˜λŠ”κ²Œ μΌλ°˜μ μ΄μ§€λ§Œ 상황에 λ”°λΌμ„œ 클래슀 내뢀에 μž‘μ„±ν•˜λŠ” κ²½μš°λ„ μ‘΄μž¬ν•œλ‹€.

읡λͺ…λ©”μ†Œλ“œμ˜ ν™œμš©(feat. Bubble Sort)

using System.Collections;

namespace AnonymousMethodEx
{
    delegate int SelectSort(int x, int y);
    class Program
    {    
        static void BSort(int[] arr, SelectSort ss)
        {
            int temp = 0;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                for (int j = 0; j < arr.Length - (i+1); j++)
                {
                    if (ss(arr[j],arr[j+1]) > 0)
                    {
                        temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }

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

            // 읡λͺ…ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄ 델리게이트λ₯Ό μ„ μ–Έν•˜κΈ°.(μ˜€λ¦„μ°¨μˆœ)
            SelectSort ascend;
            ascend = delegate(int x, int y){
                if (x > y) return 1;
                else if (x == y) return 0;
                else return -1;
            };

            BSort(array, ascend);

            Console.WriteLine("μ˜€λ¦„μ°¨μˆœ : ");
            foreach (int x in array)
            {
                Console.Write($"{x} ");
            }
            Console.WriteLine();

            // 읡λͺ…ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄ 델리게이트λ₯Ό μ„ μ–Έν•˜κΈ°.(λ‚΄λ¦Όμ°¨μˆœ)
            SelectSort descend;
            descend = delegate (int x, int y) {
                if (x < y) return 1;
                else if (x == y) return 0;
                else return -1;
            };

            BSort(array, descend);

            Console.WriteLine("λ‚΄λ¦Όμ°¨μˆœ : ");
            foreach (int x in array)
            {
                Console.Write($"{x} ");
            }
            Console.WriteLine();

            // 더 쒋은 λ°©λ²•μœΌλ‘œ μ „λ‹¬ν•˜λŠ” λ©”μ†Œλ“œμ—μ„œ λ°”λ‘œ μž‘μ„±ν•  수 있음
            int[] array2 = { 3, 2, 4, 1, 10, 7, 21, 42, 5, 13 };

            BSort(array2, delegate(int x, int y)
            {
                if (x > y) return 1;
                else if (x == y) return 0;
                else return -1;
            });

            Console.WriteLine("λ©”μ†Œλ“œμ˜ μΈμžμ—μ„œ μ„ μ–Έν•œ 읡λͺ… λ©”μ†Œλ“œ μ˜€λ¦„μ°¨μˆœ : ");
            foreach (int x in array2)
            {
                Console.Write($"{x} ");
            }
            Console.WriteLine();

        }
    }
}
Output

μ˜€λ¦„μ°¨μˆœ :
1 2 4 7 10
λ‚΄λ¦Όμ°¨μˆœ :
10 7 4 2 1
λ©”μ†Œλ“œμ˜ μΈμžμ—μ„œ μ„ μ–Έν•œ 읡λͺ… λ©”μ†Œλ“œ μ˜€λ¦„μ°¨μˆœ :
1 2 3 4 5 7 10 13 21 42
  • 읡λͺ…ν•¨μˆ˜λŠ” λ‹€μ–‘ν•˜κ²Œ ν™œμš©λ  수 μžˆλ‹€. μœ„μ˜ μ½”λ“œμ—μ„œλŠ” μΈμŠ€ν„΄μŠ€λ₯Ό 생성할 λ•Œ 읡λͺ…ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜λŠ” 방식과 λ©”μ†Œλ“œμ˜ 인자둜 λ„˜κΈΈλ•Œ 읡λͺ…ν•¨μˆ˜λ₯Ό 톡해 λ°”λ‘œ μž‘μ„±ν•˜λŠ” 방식 두 κ°€μ§€λ₯Ό μ‹€μŠ΅ν•˜μ˜€λ‹€.

            // 읡λͺ…ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄ 델리게이트λ₯Ό μ„ μ–Έν•˜κΈ°.(μ˜€λ¦„μ°¨μˆœ)
            SelectSort ascend;
            ascend = delegate(int x, int y){
                if (x > y) return 1;
                else if (x == y) return 0;
                else return -1;
            };

            BSort(array, ascend);

            Console.WriteLine("μ˜€λ¦„μ°¨μˆœ : ");
            foreach (int x in array)
            {
                Console.Write($"{x} ");
            }
            Console.WriteLine();
  • 첫번째 배열에 λŒ€ν•˜μ—¬ μ˜€λ¦„μ°¨μˆœκ³Ό λ‚΄λ¦Όμ°¨μˆœ 방식이 λ˜‘κ°™μ•„ ν•˜λ‚˜λ§Œ ν™•μΈν•΄λ³΄μž
  • λ¨Όμ € SelectSort에 λŒ€ν•œ μΈμŠ€ν„΄μŠ€λ‘œ ascend μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•œ λ’€ 읡λͺ…ν•¨μˆ˜λ₯Ό 톡해 delegate둜 λ©”μ†Œλ“œλ₯Ό ν• λ‹Ήν•œ 것을 확인할 수 μžˆλ‹€.

            // 더 쒋은 λ°©λ²•μœΌλ‘œ μ „λ‹¬ν•˜λŠ” λ©”μ†Œλ“œμ—μ„œ λ°”λ‘œ μž‘μ„±ν•  수 있음
            int[] array2 = { 3, 2, 4, 1, 10, 7, 21, 42, 5, 13 };

            BSort(array2, delegate(int x, int y)
            {
                if (x > y) return 1;
                else if (x == y) return 0;
                else return -1;
            });

            Console.WriteLine("λ©”μ†Œλ“œμ˜ μΈμžμ—μ„œ μ„ μ–Έν•œ 읡λͺ… λ©”μ†Œλ“œ μ˜€λ¦„μ°¨μˆœ : ");
            foreach (int x in array2)
            {
                Console.Write($"{x} ");
            }
            Console.WriteLine();
  • λ‘λ²ˆμ§Έ λ°°μ—΄μ˜ 정렬에 λŒ€ν•΄μ„œλŠ” BSort()λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•¨κ³Ό λ™μ‹œμ— 인자둜 읡λͺ…ν•¨μˆ˜λ₯Ό μ„ μ–Έν•΄ 쀌으둜써 λ„˜κ²¨μ£Όμ—ˆλ‹€.
  • μœ„μ˜ μ½”λ“œλ₯Ό 톡해 확인할 수 μžˆλ‹€.