zoukankan      html  css  js  c++  java
  • yield return in C#

    Yield has two great uses

    1. It helps to provide custom iteration with out creating temp collections.

    2. It helps to do stateful iteration

    Iteration. It creates a state machine "under the covers" that remembers where you were on each additional cycle of the function and picks up from there.

    e.g.

    private static IEnumerable<string> GetIdList(DateTime startTime, DateTime endTime)
    {
        var collectionList = new List<string>();
    
        for (var dateTime = new DateTime(startTime.Year, startTime.Month, 1); dateTime <= endTime; dateTime = dateTime.AddMonths(1))
        {
            collectionList.Add(dateTime.ToString("d"));
        }
    
        return collectionList;
    }
    

      could be writen as:

    private static IEnumerable<string> GetIdList(DateTime startTime, DateTime endTime)
    {
        for (var dateTime = new DateTime(startTime.Year, startTime.Month, 1); dateTime <= endTime; dateTime = dateTime.AddMonths(1))
        {
            yield return collectionList.Add(dateTime.ToString("d"));
        }
    }
    

      

    1. It helps to provide custom iteration with out creating temp collections.
    2. It helps to do stateful iteration.

    3. In order to explain the above two points more demonstratively, I have created a simple video and the link for same is here: http://www.youtube.com/watch?v=4fju3xcm21M
  • 相关阅读:
    gulp模块编译
    微擎数据库表-T
    微信小程序自动识别姓名电话地址
    PHPBase64格式编码图片
    HTML中Data的数据类型
    EPP状态码
    WePay-T
    HTML-T
    PHPNamespace命名空间
    jQuery:jQuery简介
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/5044293.html
Copyright © 2011-2022 走看看