(zz)从callback的角度来理解.NET/C# 中的 委托 (delegate)与 事件 (event)
2013-08-17 14:46
796 查看
delegate是个比较生僻的单词(其实是我英语差~)。那先看看简要的来自金山词霸的解释:
代码5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateTest2
{
class Employee
{
publicdelegatevoid NoticeList(string msg);
private NoticeList MyNoticeList;
publicvoid AddToNoticeList(NoticeList np)
{
MyNoticeList += np;
}
publicvoid Notice()
{
if (MyNoticeList !=null)
{
MyNoticeList("I have finished a task!");
}
}
}
class Leader
{
}
class Manager
{
}
class Program
{
staticvoid Main(string[] args)
{
Employee XiaoLi =new Employee();
Leader LaoLiu =new Leader();
Manager AnZong =new Manager();
XiaoLi.AddToNoticeList(msg => { Console.WriteLine(msg +" Yes,i will check it!"); });
XiaoLi.AddToNoticeList(msg => { Console.WriteLine(msg +" UMM,good boy!"); });
XiaoLi.Notice();
}
}
}
注意这次我们没有用event.而是把匿名方法用Lambda表达式的方式加在了原始的私有委托列表中。
另外,Lambda表达式在LINQ中也有大量的应用!
从1.0的没有泛型,到3.5的Lambda表达式,LINQ,.NET的语法功能及编程效率在步步提高。并且也增加了学习难度,用熟了,很简洁,没上手时,往着一看,水深得吓人!
代码5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateTest2
{
class Employee
{
publicdelegatevoid NoticeList(string msg);
private NoticeList MyNoticeList;
publicvoid AddToNoticeList(NoticeList np)
{
MyNoticeList += np;
}
publicvoid Notice()
{
if (MyNoticeList !=null)
{
MyNoticeList("I have finished a task!");
}
}
}
class Leader
{
}
class Manager
{
}
class Program
{
staticvoid Main(string[] args)
{
Employee XiaoLi =new Employee();
Leader LaoLiu =new Leader();
Manager AnZong =new Manager();
XiaoLi.AddToNoticeList(msg => { Console.WriteLine(msg +" Yes,i will check it!"); });
XiaoLi.AddToNoticeList(msg => { Console.WriteLine(msg +" UMM,good boy!"); });
XiaoLi.Notice();
}
}
}
注意这次我们没有用event.而是把匿名方法用Lambda表达式的方式加在了原始的私有委托列表中。
另外,Lambda表达式在LINQ中也有大量的应用!
从1.0的没有泛型,到3.5的Lambda表达式,LINQ,.NET的语法功能及编程效率在步步提高。并且也增加了学习难度,用熟了,很简洁,没上手时,往着一看,水深得吓人!
相关文章推荐
- 从callback的角度来理解.NET/C# 中的 委托 (delegate)与 事件 (event)
- 从callback的角度来理解.NET/C# 中的 委托 (delegate)与 事件 (event)
- 从callback的角度来理解.NET/C# 中的 委托 (delegate)与 事件 (event)
- 关于C#.Net中事件(event)、委托(delegate)和方法的一点理解
- c#的委托delegate和事件Event的一步一步详细解释
- C#编程利器之四:委托与事件(Delegate and event) (上)
- 委托和事件[delegate and event]_C#
- C#事件-换一个角度理解委托和事件
- 我是如何学习设计模式的一:.NET C# 委托定天下,学好委托事件,设计模式精髓可以理解三分之一,怎么说也是三分天下之势了
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- (转)C#中的委托(Delegate)和事件(Event)
- 转:终于会用c#中的delegate(委托)和event(事件)了
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- C# 委托(delegate)事件(event)分析
- .Net: C#中的委托(Delegate)和事件(Event)
- 关于C# 委托(delegate)与事件(event)的用法及事例
- c#中的delegate(委托)和event(事件)
- c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2]