2025유니티수업

0611 C#(이벤트 핸들러, 대리자)

버스창문 2025. 6. 11. 12:56

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

namespace _06_11_CSharp
{
    delegate void MyDelegate(int a);

    delegate int MyDelegate2(int a, int b);
    class Market
    {
        public event MyDelegate CustomerEvent;

        public void BuySomthing(int CustomerNo)
        {
            if (CustomerNo == 30)
                CustomerEvent(CustomerNo);
        }

    }
    internal class Ex
    {
        
        static void Main(string[] args)
        {
            // 1번
            Market market = new Market();
            market.CustomerEvent += (a) => Console.WriteLine("!");

            for (int customerNo = 0; customerNo < 100; customerNo++)
            {
                market.BuySomthing(customerNo);
            }

            // 2번
            MyDelegate2 Callback;

            Callback = (a,b) => a+b;
            Console.WriteLine(Callback(3,4));

            Callback = delegate (int a, int b)
            {
                return a - b;
            };
            Console.WriteLine(Callback(7, 5));

        }
    }
}