C#/C#200제

[C#] 17일차 - 117. 람다식의 사용

반나무 2021. 2. 15. 18:42

람다식은 익명메소드를 간단하게 표현할 수 있는 방법.

 

1. 식 람다

연산자의 오른쪽에 식이 있는것을 식 람다라고함.

식 람다는 식의 결과를 반환하며 기본 형식은 아래와 같음

(input-parameters) => expression

 

2. 문 람다

문 람다는 다음과 같이 중괄호 안에 문장을 지정한다는 점을 제외하면 식 람다와 비슷하다.

(input-parameters) => { statement; }

 

간단한 예제 : 리턴값이 없고 매개변수가 하나인 메소드를 문 람다로 작성.

greet("World"); 라고 호출하면 "Hello World!" 라고 출력됨

Action<string> greet = name =>
{
	string greeting = $"Hello {name}!";
    Console.WriteLine(greeting);
}

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

namespace A117_LambdaExamples
{
    class Program
    {
        delegate double CalcMethod(double a, double b);
        delegate bool IsTeenAger(Student student);
        delegate bool IsAdult(Student student);

        static void Main(string[] args)
        {
            // square는 하나의 정수를 매개변수로하고 return값이 정수인 Func 델리게이트
            Func<int, int> square = x => x * x;
            Console.WriteLine(square(5));

            // 정수배열의 각 요소를 제곱해 할당한 후 합쳐서 출력
            int[] numbers = { 2, 3, 4, 5 };
            var squaredNumbers = numbers.Select(x => x * x);
            Console.WriteLine(string.Join(" ", squaredNumbers));

            // Action 델리게이트로 line() = Console.WriteLine()은 같다.
            Action line = () => Console.WriteLine();
            line();

            CalcMethod add = (a, b) => a + b;
            CalcMethod subtract = (a, b) => a - b;

            Console.WriteLine(add(10, 20));
            Console.WriteLine(subtract(10.5, 20));

            IsTeenAger isTeen = delegate (Student s) { return s.Age > 12 && s.Age < 20; };
            Student s1 = new Student() { Name = " John", Age = 18 };

            Console.WriteLine("{0}은 {1}.", s1.Name, isTeen(s1) ? "청소년입니다" : "청소년이 아닙니다.");

            IsAdult isAdult = (s) =>
            {
                int adultAge = 18;
                return s.Age >= adultAge;
            };

            Student s2 = new Student() { Name = "Robin", Age = 20 };
            Console.WriteLine("{0}은 {1}.", s2.Name, isAdult(s2) ? "성인입니다" : "성인이 아닙니다.");

        }
    }

    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

반응형