Use Action<(Of <(T>)>) delegate with anonymous methods : Action « Development « C# / CSharp Tutorial






using System;
using System.Windows.Forms;

public class TestAnonMethod
{
   public static void Main()
   {
      Action<string> messageTarget; 

      if (Environment.GetCommandLineArgs().Length > 1)
         messageTarget = delegate(string s) { ShowWindowsMessage(s); };
      else
         messageTarget = delegate(string s) { Console.WriteLine(s); };

      messageTarget("Hello, World!");
   }

   private static void ShowWindowsMessage(string message)
   {
      MessageBox.Show(message);      
   }
}








14.41.Action
14.41.1.Instantiating Action<(Of <(T>)>) delegate rather than defining a new delegate
14.41.2.Use Action<(Of <(T>)>) delegate with anonymous methods
14.41.3.Assign a lambda expression to an Action<(Of <(T>)>) delegate instance