C# 委托
发布日期:2021-05-10 05:29:46 浏览次数:16 分类:精选文章

本文共 2803 字,大约阅读时间需要 9 分钟。

���������������

���������������������������������������������������������������������������������������������������������������������if-else���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������

������������������������������new���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������GreetingDelegate������������������ChineseGreeting���������

GreetingDelegate g1 = new GreetingDelegate(ChineseGreeting);

���������������������������g1("������")���������������������������

g1("������");

������������

C#������������������������������������Action���Func���������������������������������������

  • Action������������������������������������������������������������������������������������������������������������Action

    (params T[])���������������������������������������

  • Func������������������������������������������������������������������������������������������Func

    (params T[])���������������������������������������������������������������������������

������������������������������������������������������������������

// Action������
Action
action = name => Console.WriteLine("���������" + name);
action("Tom");
// Func������
Func
calculate = (x, y) => x + y;
int result = calculate(100, 200);
// ��������� Func
Func
assertTrue = (value, expected) => value == expected;
bool result trovato = assertTrue("Hello", true);

������������

������������������������������������������������������������������������������+������������������������������������������������������-������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������

// ���������������������������������������
public delegate void DrawEvent(IDrawable drawable, string operation);
// ���������������������������������������
IDrawable d1 = new Draw { StuID = 1 };
IDrawable d2 = new Draw { StuID = 2 };
// ���������������������
DrawEvent event1 = new DrawEvent(d1, "draw cat");
DrawEvent event2 = new DrawEvent(d2, "draw dog");
DrawEvent event3 = new DrawEvent(d1, "draw dog");
// ������������������������
event1 += event2;
event1 += event3;
// ������������
event1();

������

���������������C#������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������C#���������������������������������������������

���������https://pan.baidu.com/s/19C_qxIT5uX7YAsmDU9Qckg ������������tnni

上一篇:C# .NET与数据结构
下一篇:C# 异常

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月23日 18时21分29秒