Action:相當於一種無參數傳遞的委託,從Framework 3.5開始加如,簡化了委託調用代碼:
Delegate原代碼:
using System;
using System.Windows.Forms;
public delegate void DisplayMessage();
public class testTestDelegate
{
public static void Main()
{
DisplayMessage showMethod = DisplayToWindow();
showMethod();
}
public static void DisplayToWindow()
{
MessageBox.Show("Antiquity Way:Hello World");
}
}
Action新代碼:
using System;
using System.Windows.Forms;
public class testTestDelegate
{
public static void Main()
{
Action showMethod = DisplayToWindow;
showMethod();
}
public static void DisplayToWindow()
{
MessageBox.Show("Action Way:Hello World");
}
}
更簡潔的方法:(Lambda方法)
using System;
using System.Windows.Forms;
public class testTestDelegate
{
public static void Main()
{
Action showMethod = () =>{ MessageBox.Show("Action Way:Hello World"); };
showMethod();
}
}
參考MSDN:http://msdn.microsoft.com/zh-cn/library/system.action.aspx