본문 바로가기
C#/수업내용

[ C# 9일차 ] 람다 연습

by 왹져박사 2023. 1. 11.
728x90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study10
{
    class App
    {

        //생성자
        public App()
        {
            Console.WriteLine("App");


            this.LoadFile(() =>        //문람다(익명 메서드). 
            {
                Console.WriteLine("출력");
            });
        }

        private void LoadFile(Action calback)     //매개변수에 대리자 인스턴스
        {
            calback();
        }
    }
}

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

namespace Study10
{
    class App
    {

        //생성자
        public App()
        {
            Console.WriteLine("App");


            this.LoadFile((str) =>              //람다문(익명메서드) 
            {
                //출력
                Console.WriteLine(str); //hello world!
            });
        }

        private void LoadFile(Action<string> callback)     
        {
            //파일을 로드한다(읽는다
            //(파일에 있는 문자열 값을 읽어 온다)
            string str = "hello world!";
            callback(str);
        }
    }
}

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

namespace Study10
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            this.LoadScene(() =>
            {
                Console.WriteLine("씬로드 완료");
            });
        }

        private void LoadScene(Action callback)
        {
            callback();
        }
    }
}

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

namespace Study10
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            Hero hero = new Hero();
            hero.Move(() =>
            {
                Console.WriteLine("이동 완료");
            });
        }

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

namespace Study10
{
    class Hero
    {
        //생성자
        public Hero()
        {

        }
        public void Move(Action callback)
        {
            callback();
        }
    }
}

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

namespace Study10
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            Hero hero = new Hero();
            hero.onMoveComplete = () =>
            {
                Console.WriteLine("이동 완료");
            };
            hero.Move();            //이동완료되면 대리자를 호출
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study10
{

    class Hero
    {
        public Action onMoveComplete;

        //생성자
        public Hero()
        {

        }

        public void Move()
        {
            Console.WriteLine("이동중...");
            onMoveComplete();
        }
    }
}

 

728x90