• 임시 매서드, 빈 칸

- 몇 번 안 쓸 거 같을 때 명령어만 넣고 이건 매서드야~ 알지? 라고 알려줌

1. 대리자에 매서드 넣기 = 대리자 Delegate 사용 복습
- 익명 매서드를 만들려면 대리자가 필요해.

 

  • Main
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NumCompare
{

    internal class Program
    {
        // 대리자에 매서드 넣기
        delegate int Mydelegate(int a, int b);

        static void Main(string[] args)
        {
            App app = new App();
            Mydelegate my;

            //// 기본형
            //app.Minus(5,3);
            //app.Plus(5,3);

            my = app.Plus;
            my(7,6);

            my = app.Minus;
            my(7,6);
        }
    }
}

 
 

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

namespace NumCompare
{
    internal class App
    {
        public App()
        {
          //  Console.WriteLine("App생성자 실행");
        }

        // 숫자 연산
        public int Minus(int a, int b)
        {
            Console.WriteLine($"{a}-{b}={a - b}");
            return a - b;
        }


        public int Plus(int a, int b)
        {
            Console.WriteLine($"{a}+{b}={a + b}");
            return a + b;
        }
    }
}




2. 매서드 넣는데에 delegate 선언하고 메서드가 있는 거처럼 사용
- 대리자는 변수에 넣어서 인스턴스로 만들어서 쓴다.
⭐️{  };⭐️ 이면 학실히 임시 매서드다. ;;;;;;;;;;;;;;
또 앞글자가 d, 소문자면 매서드는 보통 대문자 이름이고 이름은 중요한데 이거는 임신갑다…

 

  • Main
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Num2
{
    internal class Program
    {
        static int a;
        static int b;

        delegate int Mydelegate(int a, int b);

        static void Main(string[] args)
        {
            Mydelegate my;

            my = delegate (int a, int b)
            {
                Console.Write($"{a}+{b}=");
                return a + b; 
            };
            Console.WriteLine(my(5,4));


            my = delegate (int a, int b)
            {
                Console.Write($"{a}-{b}=");
                return a - b;
            };
            Console.WriteLine(my(5,4));
        }
    }
}

 
ㅎㅎㅎ; c# 너무 좋아서 내가 잘못하고 있는대도 막 답인 거처럼 받아준다 ㅋㅋㅋ
 
- 참고로 이런 식도 가능...

  • Main
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Num2
{
    internal class Program
    {
        static int a;
        static int b;

        delegate int Mydelegate(int a, int b);

        static void Main(string[] args)
        {
            Mydelegate my;
            a = 9; // 변수 값을 미리 할당해도 가능!
            b = 5;

            my = delegate (int a, int b)
            {
                Console.Write($"{a}+{b}=");
                return a + b; 
            };
            Console.WriteLine(my(a,b));


            my = delegate (int a, int b)
            {
                Console.Write($"{a}-{b}=");
                return a - b;
            };
            Console.WriteLine(my(a,2));
        }
    }
}

+ Recent posts