您的位置:首页 > 编程语言 > C#

C# Action<T> 使用

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

usingSystem;

usingSystem.Windows.Forms; 

delegatevoid DisplayMessage(stringmessage); 

publicclass TestCustomDelegate{

   publicstatic void Main()

   {

      DisplayMessage messageTarget;  

      if(Environment.GetCommandLineArgs().Length > 1)

         messageTarget = ShowWindowsMessage;

      else

         messageTarget = Console.WriteLine; 

      messageTarget("Hello, World!");  

   }     

 

   privatestatic void ShowWindowsMessage(stringmessage)

   {

      MessageBox.Show(message);     

   }

}

 

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

usingSystem;

usingSystem.Windows.Forms; 

publicclass TestAction1

{

   publicstatic void Main()

   {

      Action<string> messageTarget; 

      if(Environment.GetCommandLineArgs().Length > 1)

         messageTarget = ShowWindowsMessage;

      else

         messageTarget = Console.WriteLine; 

      messageTarget("Hello, World!");  

   }     

 

   privatestatic void ShowWindowsMessage(stringmessage)

   {

      MessageBox.Show(message);     

   }

}

 

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

usingSystem;

usingSystem.Windows.Forms;

 

publicclass TestAnonMethod

{

   publicstatic void Main()

   {

      Action<string> messageTarget;  

      if(Environment.GetCommandLineArgs().Length > 1)

         messageTarget = delegate(strings) { ShowWindowsMessage(s); };

      else

         messageTarget = delegate(strings) { Console.WriteLine(s); }; 

      messageTarget("Hello, World!");

   }

 

   privatestatic void ShowWindowsMessage(stringmessage)

   {

      MessageBox.Show(message);     

   }

}

 

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

usingSystem;

usingSystem.Windows.Forms;

 

publicclass TestLambdaExpression

{

   publicstatic void Main()

   {

      Action<string> messageTarget; 

 

      if(Environment.GetCommandLineArgs().Length > 1)

         messageTarget = s => ShowWindowsMessage(s); 

      else

         messageTarget = s => Console.WriteLine(s);

 

      messageTarget("Hello, World!");

   }

 

   privatestatic void ShowWindowsMessage(stringmessage)

   {

      MessageBox.Show(message);     

   }

}

 

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

usingSystem;

usingSystem.Collections.Generic;

 

classProgram

{

    staticvoid Main()

    {

        List<String> names = newList<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);

        });

    }

 

    privatestatic void Print(strings)

    {

        Console.WriteLine(s);

    }

}

/* This code will produce output similar to the following:

 * Bruce

 * Alfred

 * Tim

 * Richard

 * Bruce

 * Alfred

 * Tim

 * Richard

 */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: