When using the action <t> delegate, you do not need to explicitly define a delegate for a method that encapsulates only one parameter. The following code explicitly declares a delegate named displaymessage and assigns a reference to the writeline method or the showwindowsmessage Method to the delegated instance.
using System;using System.Windows.Forms;delegate void DisplayMessage(string message);public class TestCustomDelegate{ public static void Main() { DisplayMessage messageTarget; if (Environment.GetCommandLineArgs().Length > 1) messageTarget = ShowWindowsMessage; else messageTarget = Console.WriteLine; messageTarget("Hello, World!"); } private static void ShowWindowsMessage(string message) { MessageBox.Show(message); }}
The following Code simplifies the process by instantiating the action <t> delegate instead of explicitly defining a new delegate and assigning the namemethod to the delegate.
using System;using System.Windows.Forms;public class TestAction1{ public static void Main() { Action<string> messageTarget; if (Environment.GetCommandLineArgs().Length > 1) messageTarget = ShowWindowsMessage; else messageTarget = Console.WriteLine; messageTarget("Hello, World!"); } private static void ShowWindowsMessage(string message) { MessageBox.Show(message); }}
You can also use the action <t> delegate with the anonymous method.
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); }}
You can also assign a Lambda expression to an action <t> delegate instance.
using System;using System.Windows.Forms;public class TestLambdaExpression{ public static void Main() { Action<string> messageTarget; if (Environment.GetCommandLineArgs().Length > 1) messageTarget = s => ShowWindowsMessage(s); else messageTarget = s => Console.WriteLine(s); messageTarget("Hello, World!"); } private static void ShowWindowsMessage(string message) { MessageBox.Show(message); }}
The following uses the action <t> delegate to print the content of the list <t> object. Use the print method to display the list content to the console. In addition, the content is displayed on the console anonymously. Note that this example does not explicitly declare the action <t> variable. Instead, it transmits a reference to a method. This method uses a single parameter and does not return the value to the list <t>. foreach method. A single parameter is an action <t> delegate. Similarly, in the C # example, the action <t> delegate is not explicitly instantiated because the signature of the anonymous method matches the list <t>. the signature of the expected action <t> delegate in the foreach method.
using System;using System.Collections.Generic;class Program{ static void Main() { List<String> names = new List<String>(); names.Add("Bruce"); names.Add("Alfred"); names.Add("Tim"); names.Add("Richard"); // Display the contents of the list using the Print method. names.ForEach(Print); // The following demonstrates the anonymous method feature of C# // to display the contents of the list to the console. names.ForEach(delegate(String name) { Console.WriteLine(name); }); } private static void Print(string s) { Console.WriteLine(s); }}/* This code will produce output similar to the following: * Bruce * Alfred * Tim * Richard * Bruce * Alfred * Tim * Richard */