C # action delegate

Source: Internet
Author: User

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 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.