Is yield useful outside of LINQ?

后端 未结 14 936
野性不改
野性不改 2020-12-24 06:44

When ever I think I can use the yield keyword, I take a step back and look at how it will impact my project. I always end up returning a collection instead of yeilding becau

14条回答
  •  不思量自难忘°
    2020-12-24 07:24

    yield was developed for C#2 (before Linq in C#3).

    We used it heavily in a large enterprise C#2 web application when dealing with data access and heavily repeated calculations.

    Collections are great any time you have a few elements that you're going to hit multiple times.

    However in lots of data access scenarios you have large numbers of elements that you don't necessarily need to pass round in a great big collection.

    This is essentially what the SqlDataReader does - it's a forward only custom enumerator.

    What yield lets you do is quickly and with minimal code write your own custom enumerators.

    Everything yield does could be done in C#1 - it just took reams of code to do it.

    Linq really maximises the value of the yield behaviour, but it certainly isn't the only application.

提交回复
热议问题