문제/C# 연습문제

C# 연습문제 : 람다식 (뇌를 자극하는 C#5.0)

코딩너구리 2022. 9. 21. 23:07
using System;

namespace BookTestC
{
    public class Program
    {
        private static void Main(string[] args)
        {
            int[] array = { 11, 22, 33, 44, 55 };

            Func<int, int> func_1 = (x) => x * x;

            foreach (int a in array)
            {
                Console.WriteLine(func_1(a));
            }
        }
    }
}

1. 다음 코드의 출력 결과값은 얼마일까요?

   Func<int> func_1 = () => 10;
   Func<int, int> func_2 = (a) => a * 2;

   Console.WriteLine(func_1() + func_2(30));

Func 제네릭 대리자는 0개 이상의 인수를 가지고, 마지막에 반환 타입을 가진다.
func_1 = 10

func_2 = 30 *2

= 70

 

2. 다음 코드에서 익명 메소드를 람다식으로 수정하세요

using System;

namespace BookTestC
{
    public class Program
    {
        private static void Main(string[] args)
        {
            int[] array = { 11, 22, 33, 44, 55 };
            foreach (int a in array)
            {
                Action action = new Action
                (
                    delegate ()
                    {
                        Console.WriteLine(a * a);
                    }
                );

                action.Invoke();
            }
        }
    }
}
using System;

namespace BookTestC
{
    public class Program
    {
        private static void Main(string[] args)
        {
            int[] array = { 11, 22, 33, 44, 55 };

            Func<int, int> func_1 = (x) => x * x;

            foreach (int a in array)
            {
                Console.WriteLine(func_1(a));
            }
        }
    }
}