温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

委托代理action 的四种方式

发布时间:2020-07-17 09:20:04 来源:网络 阅读:401 作者:hsapphire 栏目:编程语言

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);
}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI