• C#


    C# Action<T> 委托

    在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的方法的委托。以下代码显式声明了一个名为 DisplayMessage 的委托,并将对 WriteLine 方法或 ShowWindowsMessage 方法的引用分配给其委托实例。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    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);     
       }
    }

    以下简化了此代码,它所用的方法是实例化 Action<T> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    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);     
       }
    }

    也可以将 Action<T> 委托与匿名方法一起使用。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    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);     
       }
    }

    也可以将 lambda 表达式分配给 Action<T> 委托实例。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    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);     
       }
    }

    下面使用 Action<T> 委托来打印 List<T> 对象的内容。 使用 Print 方法将列表的内容显示到控制台上。 此外还使用匿名方法将内容显示到控制台上。 请注意该示例不显式声明 Action<T> 变量。 相反,它传递方法的引用,该方法采用单个参数而且不将值返回至 List<T>.ForEach 方法,其单个参数是一个 Action<T> 委托。 同样,在 C# 示例 中,Action<T> 委托不被显式地实例化,因为匿名方法的签名匹配 List<T>.ForEach 方法所期望的 Action<T> 委托的签名。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    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
     */
    C#
     
     
     
    .NET多线程编程
    posted @ 2011-01-03 17:37 Melody Luo 阅读(498) | 评论 (0) 编辑
     
    C# Action<T> 委托
    posted @ 2011-01-01 21:34 Melody Luo 阅读(1247) | 评论 (4) 编辑
     
    C# 引用参数
    posted @ 2010-12-19 11:01 Melody Luo 阅读(144) | 评论 (0) 编辑
     
     
     
     
     
     
     
     
  • 相关阅读:
    01-发送你的第一个请求
    postman使用
    java poi导出多sheet页
    base64加密解密
    Django crontab
    super().__init__()
    paramiko模块
    列表转json数据返回
    socket模块判断ip加端口的连通性
    登录拦截器
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2313353.html
Copyright © 2020-2023  润新知