What are real life applications of yield?

前端 未结 7 1849
梦谈多话
梦谈多话 2020-12-15 23:12

I know what yield does, and I\'ve seen a few examples, but I can\'t think of real life applications, have you used it to solve some specific problem?

(I

7条回答
  •  甜味超标
    2020-12-15 23:29

    Another good use for yield is to perform a function on the elements of an IEnumerable and to return a result of a different type, for example:

    public delegate T SomeDelegate(K obj);
    
    public IEnumerable DoActionOnList(IEnumerable list, SomeDelegate action)
    {
        foreach (var i in list)
            yield return action(i);
    }
    

提交回复
热议问题