‘yield 返回’的具体类型是什么?返回?
这个 IEnumerable
的具体类型是什么?
private IEnumerable<string> GetIEnumerable()
{
yield return "a";
yield return "a";
yield return "a";
}
What is the concrete type for this IEnumerable<string>
?
private IEnumerable<string> GetIEnumerable()
{
yield return "a";
yield return "a";
yield return "a";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是编译器生成的类型。编译器生成一个返回三个“a”值的
IEnumerator
实现,以及一个在其GetEnumerator< 中提供其中之一的
IEnumerable
骨架类。 /代码> 方法。生成的代码看起来像这样*:
或者,正如 SLaks 在他的回答中所说,这两个实现最终在同一个类中。我根据我之前看过的生成代码的断断续续的记忆写了这篇文章;实际上,一个类就足够了,因为上述功能没有理由需要两个类。
事实上,仔细想想,这两个实现确实应该属于一个类,因为我刚刚记得使用
yield
语句的函数必须有一个返回类型或者IEnumerable
或IEnumerator
。不管怎样,我会让你对我在心里发布的内容进行代码更正。
*这纯粹是为了说明目的;我不保证其真实准确性。这只是根据我在自己的调查中看到的证据,以一般方式演示编译器如何执行其操作。
It's a compiler-generated type. The compiler generates an
IEnumerator<string>
implementation that returns three "a" values and anIEnumerable<string>
skeleton class that provides one of these in itsGetEnumerator
method.The generated code looks something like this*:
Or maybe, as SLaks says in his answer, the two implementations end up in the same class. I wrote this based on my choppy memory of generated code I'd looked at before; really, one class would suffice, as there's no reason the above functionality requires two.
In fact, come to think of it, the two implementations really should fall within a single class, as I just remembered the functions that use
yield
statements must have a return type of eitherIEnumerable<T>
orIEnumerator<T>
.Anyway, I'll let you perform the code corrections to what I posted mentally.
*This is purely for illustration purposes; I make no claim as to its real accuracy. It's only to demonstrate in a general way how the compiler does what it does, based on the evidence I've seen in my own investigations.
编译器将自动生成一个同时实现
IEnumerable
和IEnumerator
(在同一个类中)的类。Jon Skeet 有详细的解释。
The compiler will automatically generate a class that implements both
IEnumerable<T>
andIEnumerator<T>
(in the same class).Jon Skeet has a detailed explanation.
该方法返回的
IEnumerable
的具体实现是编译器生成的匿名类型Prints :
The concrete implementation of
IEnumerable<string>
returned by the method is an anonymous type generated by the compilerPrints :