728x90
반응형

[예제1]

lamda Expression

using System;
using static System.Console;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 익명 함수 사용 방법
            Cal cal1 = delegate (int a, int b)
            {
                return a + b;
            };

            // 람다식
            Cal cal2 = (a, b) =>
            {
                return a + b;
            };

            WriteLine(cal2(3, 4));

            // 실행문이 단문장일때 {} 를 생략할 수 있는데 이때 return도 같이 생략해야 한다.
            Cal cal3 = (a, b) => a + b; // 위 cal2와 동일문장
            WriteLine(cal3(3, 4));

            DoSomething do1 = () =>
            {
                WriteLine("call");
            };
            do1();
        }
        delegate int Cal(int a, int b);
        delegate void DoSomething();
    }
}

 


[예제2]

using System;
using static System.Console;
namespace ConsoleApp1
{    
    class Program
    {
        delegate string Concat(string[] args);

        static void Main(string[] args)
        {
            Concat concat = (xxx) =>
            {
                string r = "";
                foreach (var item in xxx)
                {
                    r += item;
                }
                return r;
            };
            string[] ar = { "호랑이", "코끼리", "독수리" };
            WriteLine(concat(ar));

        }
        
    }
}


[예제3]

using System;
using static System.Console;
namespace ConsoleApp1
{
    class Program
    {
        delegate int Test01();
        delegate int Test02(int a);
        delegate int Test03(int a, int b);

        static void Main(string[] args)
        {
            Test01 t1 = () =>
            {
                return 10;
            };
            Test02 t2 = (a) =>
            {
                return a;
            };
            Test03 t3 = (a, b) =>
            {
                return a + b;
            };

            // 리턴이 int 임을 알린다.
            // Func delegate
            Func<int> f1 = () =>
            {
                return 20;
            };
            WriteLine(f1());

            Func<string> f2 = () =>
            {
                WriteLine("호랑이");
                return "tiger";
            };

            Func<string> f3 = () =>
            {
                WriteLine("호랑이");
                return "tiger";
            };

            Func<string> f4 = () => "tiger";

            // <전달인수 타입, 리턴 타입>
            Func<int, string> f5 = (a) =>
            {
                WriteLine(a);
                return "tiger";
            };
            WriteLine(f5(10));

            Func<string, string, int> f6 = (a, b) =>
            {
                WriteLine(a + b);
                return 100;
            };
            WriteLine(f6("호랑이", "독수리"));

            Func<string, string, int> f7 = (a, b) => 100;
            WriteLine(f7("호랑이", "독수리"));
            // 인수 전달은 총 16개까지 가능

            // 리턴이 없는 delegate는 Action delegate
            // 에러 이유 (return이 생략된 것이기 때문에 에러)
            //Action<> f8 = () => 100;
            Action f8 = () => { WriteLine("호랑이"); };
            Action<int, int, string> f9 = (a, b, c) =>
            {
                WriteLine(a + b + c);
            };
            f9(10, 20, "test");
        
        }       
    }
}

728x90
반응형

'Education > Edu | .net' 카테고리의 다른 글

# 31) [C#] Winform 1  (0) 2021.02.26
# 30) [C#] 문법13 (thread)  (0) 2021.02.26
# 29.1) [C#] 문법12 (delegate)  (0) 2021.02.24
# 28.2) [C#] 문법11 (Generic)  (0) 2021.02.23
# 28.1) [C#] 문법10  (0) 2021.02.23

+ Recent posts