C#

C# - 람다식

마루설아 2024. 12. 12. 21:57

익명 메서드를 만들 때 사용

 

using System;

delegate int Add(int a, int b);

class CSharp_Practice
{
     public static void Main(String[] args)
     {
          // 람다식 기본형 (대리자 선언)
          Add add = (a, b) => a + b;
          Console.WriteLine(add(2, 3));

          Console.WriteLine("--------------");

          // 람다식 Func 대리자
          Func<int, int> square = x => x * x;
          Console.WriteLine(square(5));

          Console.WriteLine("--------------");

          // 람다식 Action 대리자
          Action<string> hello = name =>
          {
               string msg = "Hello, " + name;
               Console.WriteLine(msg);
          };

          hello("World!");
     }
}