the question itself - when within the bounds of rationality - one cannot do without this operator

  • This is, in fact, Snax sugar, so you can always do without it, but the code is shorter and clearer with it - tym32167
  • when you need to iterate, but you can do without it - Yaroslav

1 answer 1

With the help of yield you can get the result without calculating other results ... Suppose there is a certain collection that needs to be generated by some tricky algorithm .... So, with the help of yield you can get the result without generating the entire collection at once. And generate only the desired item when you refer to it.

This will save resources rather well if the collection is especially large or if the receipt of each object is long (for example, it is tied to a call to the server, which creates a significant delay).

Examples can be found here: What is the use of yield?

  • Well, you just described what this yield does, but you didn’t answer the question - когда в рамках разумности - нельзя обойтись без этого оператора ? - tym32167
  • but what is this ?: Это неплохо сэкономит ресурсы если коллекция особенно велика или же если получение каждого обьекта долгое (например привязано к обращению к серверу, что создает значительную задержку). - Andrew
  • one
    This can be solved without yield, just throwing your iterator (enumerator) - tym32167
  • The same can be said about the benefits of do while and foreach and for when there is a goto (yes, it also has sharps), but you use cycles for some reason :) The question was "in which cases it is necessary and more convenient" and I answered to this question :) - Andrew
  • Well, it’s all clear, I’m not arguing (see my comment above), you just didn’t say anything in the answer. That is, the question of когда без yield нельзя обойтись , you simply describe what this yield is, that is, IMHO the answer does not agree with the question - tym32167