What is the name of the “yield return” concept? IE

2019-08-03 23:48发布

What exactly is the official name of the yield return concept?

public IEnumerable<T> Bar() {
    ...
    yield return foo;
    ...
}

I've heard(read) it being referred to as:

  • Iterator
  • IEnumerabe
  • Generator
  • <your choice>

Isn't an iterator just some "thing" that iterates over something? A List<T> is an IEnumerable<T>, so doesn't seem reasonable as well.

A lot of answers here on SO that talk about yield return use one of these two terms.

What about "generators"? Or does that only apply when you're "conjuring up values out of thin air", e.g. the Fibonacci Numbers where you don't need a data collection as a source?

3条回答
欢心
2楼-- · 2019-08-04 00:20

The principal it is useful for though is "Coroutine". See Wikipedia.

Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations. Coroutines are well-suited for implementing more familiar program components such as cooperative tasks, iterators, infinite lists and pipes.

查看更多
小情绪 Triste *
3楼-- · 2019-08-04 00:21

It's called an iterator, check out this MSDN page

An iterator is a method, get accessor, or operator that performs a custom iteration over an array or collection class by using the yield keyword

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-08-04 00:24

As a general concept in a non-language specific way, "generator" is probably the most common term. That's what I'd use if I were talking to someone who wasn't familiar with C#.

The method itself is an iterator in C# specification terminology. The implementation is an iterator block.

From section 10.14 of the C# 4 spec:

A function member (§7.5) implemented using an iterator block (§8.2) is called an iterator.

That's the terminology I'd use when talking about C# specifically.

查看更多
登录 后发表回答