# NET中*延迟*特性的几个陷阱

NET发展至今，其实各处都有“延迟（Lazy）”的痕迹，一个小小的“Laziness”给我们带来了不少灵活性1。“延迟”的关键就在于“只在需要的时候处理数据”，老赵曾经在多篇文章中提到了类似的概念，如《高阶函数、委托与匿名方法》及《您善于使用匿名函数吗？》。不过“延迟”本身也会给您带来一些陷阱，某些陷阱您很有可能也曾经遇到过。这篇文章便是总结了延迟特性的集中常见陷阱，并给出应对方案。

“延迟”的本意是“减少计算”，但是如果您使用不当，很可能反而会造成“重复计算”。例如，我们首先构建一个方法，它接受一个参数n，返回一个Func<int, bool>对象：

 以下为引用的内容： static Func DivideBy(int n){    return x =>    {        bool divisible = x % n == 0;        Console.WriteLine(            "{0} can be divisible by {1}? {2}",            x, n, divisible ? "Yes" : "No");        return divisible;    };}

 以下为引用的内容： List values = new List();for (int i = 0; i < 10; i++) values.Add(i); var divideByTwo = values.Where(DivideBy(2));var divideByTwoAndThree = divideByTwo.Where(DivideBy(3));var divideByTwoAndFive = divideByTwo.Where(DivideBy(5)); foreach (var i in divideByTwoAndThree) { }foreach (var i in divideByTwoAndFive) { }

 以下为引用的内容： 0 can be divisible by 2? Yes0 can be divisible by 3? Yes1 can be divisible by 2? No2 can be divisible by 2? Yes2 can be divisible by 3? No3 can be divisible by 2? No4 can be divisible by 2? Yes4 can be divisible by 3? No5 can be divisible by 2? No6 can be divisible by 2? Yes6 can be divisible by 3? Yes7 can be divisible by 2? No8 can be divisible by 2? Yes8 can be divisible by 3? No9 can be divisible by 2? No0 can be divisible by 2? Yes0 can be divisible by 5? Yes1 can be divisible by 2? No2 can be divisible by 2? Yes2 can be divisible by 5? No3 can be divisible by 2? No4 can be divisible by 2? Yes4 can be divisible by 5? No5 can be divisible by 2? No6 can be divisible by 2? Yes6 can be divisible by 5? No7 can be divisible by 2? No8 can be divisible by 2? Yes8 can be divisible by 5? No9 can be divisible by 2? No

 以下为引用的内容： var divideByTwo = values.Where(DivideBy(2)).ToList();var divideByTwoAndThree = divideByTwo.Where(DivideBy(3));var divideByTwoAndFive = divideByTwo.Where(DivideBy(5));

 以下为引用的内容： 0 can be divisible by 2? Yes1 can be divisible by 2? No2 can be divisible by 2? Yes3 can be divisible by 2? No4 can be divisible by 2? Yes5 can be divisible by 2? No6 can be divisible by 2? Yes7 can be divisible by 2? No8 can be divisible by 2? Yes9 can be divisible by 2? No0 can be divisible by 3? Yes2 can be divisible by 3? No4 can be divisible by 3? No6 can be divisible by 3? Yes8 can be divisible by 3? No0 can be divisible by 5? Yes2 can be divisible by 5? No4 can be divisible by 5? No6 can be divisible by 5? No8 can be divisible by 5? No

 以下为引用的内容： public static IEnumerable ToString(IEnumerable source){    if (source == null)    {        throw new ArgumentNullException("source");    }     foreach (int item in source)    {        yield return item.ToString();    }}

 以下为引用的内容： static void Main(string[] args){    IEnumerable values;    try    {        values = ToString(null);    }    catch (ArgumentNullException)    {        Console.WriteLine("Passed the null source");        return;    }     foreach (var s in values) { }}

 以下为引用的内容： public static IEnumerable ToString(IEnumerable source){    if (source == null)    {        throw new ArgumentNullException("source");    }     return ToStringInternal(source);} private static IEnumerable ToStringInternal(IEnumerable source){    foreach (int item in source)    {        yield return item.ToString();    }}

 以下为引用的内容： List actions = new List();for (int i = 0; i < 10; i++){    actions.Add(() => Console.WriteLine(i));} foreach (var a in actions) a();

 以下为引用的内容： List actions = new List();for (int i = 0; i < 10; i++){    int  j = i; // 新增代码    actions.Add(() => Console.WriteLine(j));} foreach (var a in actions) a();

posted @ 2009-06-12 23:17  网者风  阅读(300)  评论(1编辑  收藏  举报