Code/C#

[C#] Lambda Expression vs Anonymous Method

Hide Code 2008. 2. 11. 15:42
Anonymous Method와 Lambda Expression을 비교해보자.

Lambda Expression이 조금 더 단순해보인다.


using System;

namespace DelegateSample
{
   class Program
   {
       static void Main(string[] args)
       {
           Human Tom = new Human();

           // Anonymous Method
           Tom.ActionDelegate += delegate(string text) { Console.WriteLine("Anonymous:" + text); };

           // Lambda Expression
           Tom.ActionDelegate += (string text) => Console.WriteLine("Lambda1:" + text);
           Tom.ActionDelegate += text => Console.WriteLine("Lambda2:" + text);

           Tom.ActionDelegate("Hello");
       }

       delegate void Action(string words);

       class Human
       {
           public Action ActionDelegate;
       }
   }
}