上一章節介紹Lambada表達式的使用:https://blog.csdn.net/u013251413/article/details/89182449
表達式格式:
(input parameters) => {statement;} 類似於匿名函數的用法
namespace 測試
{
delegate void myFunction1(string showNmae);
class Test1
{
static void Main(string[] args)
{
GotoMyLambda2();
}
public static void GotoMyLambda2() {
myFunction1 function1 = name =>
{
string showName = "歡迎你," + name;
Console.WriteLine(showName);
};//執行的語句
function1("小王");
Console.ReadKey();
}
}
}
下面這中更貼近:
public void TestAction() {
ShowDataAction(() =>
{
MessageBox.Show("調用了");
},//方法體
"測試之前調用");
};
public void ShowDataAction(Action action, string messages) //action接受方法體
{
try
{
MessageBox.Show(messages);
action();
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
以上是個人蔘考文檔所做的筆記,如果存在問題,望大家指出謝謝。