본문 바로가기
KDT/C# 프로그래밍

23/07/27 람다

by 잰쟁 2023. 7. 27.
728x90

람다

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

namespace starcraft
{
    public class App
    {
        //1. 대리자 형식 정의
        public delegate void DelHeroMoveComplete();

        public App()
        {
            //2. 대리자 인스턴스화 
            DelHeroMoveComplete del = () => {
                Console.WriteLine("영웅이 이동을 완료!!");
            };

            Hero hero = new Hero();
            hero.Move(del);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace starcraft
{
    internal class Hero
    { 
        
        //생성자
        public Hero()
        {
            Console.WriteLine("영웅을 생성했습니다.");
        }

        public void Move(App.DelHeroMoveComplete callback)
        {
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동완료");

            callback();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using starcraft;

namespace starcraft
{
    public class App
    {

        public App()
        {
            //대리자 변수 정의
            Action action;

            //대리자 인스턴스화 (메서드 연결)
            action = this.SayHello;
            action = () => {
                Console.WriteLine("Hello~");
            }; //람다문

            action();
        }
        void SayHello()
        {
            Console.WriteLine("안녕하세요");
        }
    }
}