[提个醒] C#中yield return的小缺点

先来看一个代码: 

using  System;
using  System.Collections.Generic;

namespace  YeildReturn
ExpandedBlockStart.gifContractedBlock.gif
{
    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            GetNotEmptyItems(
new string[] "a""""b" });
        }


        
private static IEnumerable<string> GetNotEmptyItems(IEnumerable<string> args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach (string arg in args)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Console.WriteLine(arg);
                
if (string.IsNullOrEmpty(arg))
                    
yield return arg;
            }

        }

    }

}

大家猜猜会有几行输出?2行?3行?

 

答案是没有输出。明明输入的string列表里的每个itemConsole.WriteLine了,为什么会没有输出呢?

想要有输出,要改改代码,下面的代码就可以了。

using  System;
using  System.Collections.Generic;

namespace  YeildReturn
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            
foreach (string str in  GetNotEmptyItems( new string[] { "a""""b" }) ) ;
        }

        
private   static  IEnumerable < string >  GetNotEmptyItems(IEnumerable < string >  args)
        {
            
foreach  ( string  arg  in  args)
            {
                Console.WriteLine(arg);
                
if  ( string .IsNullOrEmpty(arg))
                    
yield   return  arg;
            }
        }
    }
}

 

把对GetNotEmptyItems的调用,放在一个foreach语句中,然后什么也不做。结果就有输出了!

 

有时写代码,返回值只是为了方便组成链式表达式,使用yield是为了提高性能(^_^参考PS)和简化代码。所以很可能出现这样的情况,只调用函数,而不使用其返回值。但是这时就不应该用yield了,否则就会造成上面的问题。因为yield是要和foreach一起用的。

 

这里只是给大家提醒,想要打破沙锅问到底的朋友们可以参考网上丰富的yield的资料。下面随便找了两个园子里的链接。

C# 2.0 新特性之迭代器, Yield Return

迭代器(yield return

 

PS:几乎所有的对yield return和直接return list的测试结果都会告诉你,yield return更慢。但是当你真正了解yield之后,相信多数情况下都会选择yield return的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值