1.
Action<T>Delegate
个人理解:和func一样,但是没有返回值
Eg1: 传统型
delegatevoid DisplayMessage(string message);
publicclassTestCustomDelegate
{
publicstaticvoid Main()
{
DisplayMessage messageTarget;
if(Environment.GetCommandLineArgs().Length > 1)
messageTarget =ShowWindowsMessage;
else
messageTarget =Console.WriteLine;
messageTarget("Hello,World!");
}
privatestaticvoid ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
Eg2;
publicstaticvoid Main()
{
Action<string> messageTarget;
if(Environment.GetCommandLineArgs().Length > 1)
messageTarget =ShowWindowsMessage;
else
messageTarget =Console.WriteLine;
messageTarget("Hello,World!");
}
privatestaticvoid ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
Eg3:
publicstaticvoid 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!");
}
privatestaticvoid ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
Eg4:lamba
publicstaticvoid Main()
{
Action<string> messageTarget;
if(Environment.GetCommandLineArgs().Length > 1)
messageTarget = s =>ShowWindowsMessage(s);
else
messageTarget = s =>Console.WriteLine(s);
messageTarget("Hello,World!");
}
privatestaticvoid ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。