[导入]Why does C#'s iterators feature spit out a class definition instead of a struct definition?

简介:

Q: Why does C#'s iterators feature spit out a class definition instead of a struct definition?
 
The iterators feature in C# generates classes that implement the enumerators required. This is detailed in the C# Specification. Why doesn't it use structs, which would be more efficient.
 
A:
 
There are two reasons.
 
(1) Naming. We generate classes that implement the enumerator interfaces and then use only the interface types in the public protocol. That way the names of the generated classes are purely an implementation detail. This is highly desirable from a versioning perspective. With a struct-based implementation, to get any of the efficiencies associated with structs we would have to use their types in the public protocol (using interfaces the structs would just get boxed). That in turns means we'd have to invent a name mangling scheme for the structs. In particular, iterators returning IEnumerable<T> would be complicated because a type could have multiple such members that differ only in their parameter list, meaning that the parameter list would have to be part of the mangled name.
 
(2) Structs don't work in recursive cases. For example, a TreeNode type could implement an iterator that recursively iterates first the left and then the right subtrees by foreach-ing contained members that are also of type TreeNode. With a struct-based implementation this would translate into an enumerator struct that contains a field of its own type--which isn't possible. (Think of it this way: A foreach statement obtains an enumerator and stores that in a local variable. In iterators, local variables are transformed into fields in the enumerator. A recursive iterator would therefore create a struct with a member of its own type.) You could argue that we can detect whether or not iterators are recursive and adjust our code generation scheme accordingly. However, you then end up with a versioning problem when a previously non-recursive iterator changes its (supposedly private) implementation to become recursive.
 
Anders (via Eric)




本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/09/22/45405.html,如需转载请自行联系原作者
相关文章
|
2月前
|
C#
C#系列之ref和out的区别
C#系列之ref和out的区别
|
2月前
|
存储 C#
C#.Net筑基-类型系统②常见类型--结构体类型Struct
本文介绍了C#中的结构体(struct)是一种用户自定义的值类型,适用于定义简单数据结构。结构体可以有构造函数,能定义字段、属性和方法,但不能有终结器或继承其他类。它们在栈上分配,参数传递为值传递,但在类成员或包含引用类型字段时例外。文章还提到了`readonly struct`和`ref struct`,前者要求所有字段为只读,后者强制结构体存储在栈上,适用于高性能场景,如Span和ReadOnlySpan。
|
11月前
|
存储 C# C++
从C++角度讲解C#Out和ref的区别
从C++角度讲解C#Out和ref的区别
|
10月前
|
C#
C#中out和ref之间的区别
C#中out和ref之间的区别
|
12月前
|
C#
C#基础⑧——方法(函数、重载、out、ref)
比喻成职能。比喻成一个生产自行车老板,一个地方专门放螺丝,一个地方专门放轮,一个地方专门放车链子,需要组装什么就从那个仓库里面拿就行了。各司其职。
|
关系型数据库 MySQL C#
【C#】【MySQL】【GridView】删除出现Parameter index is out of range
【C#】【MySQL】【GridView】删除出现Parameter index is out of range
104 0
【C#】【MySQL】【GridView】删除出现Parameter index is out of range
C# ref out的使用与区别
ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。Ref型参数引入前必须赋值。 out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字。 Out型参数引入前不需赋值,赋值也没用。
133 0
C# ref out的使用与区别
|
存储 C# C++
C# 结构体(Struct)、 枚举(Enum)
C# 结构体(Struct) 在 C# 中,结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。struct 关键字用于创建结构体。 结构体是用来代表一个记录。假设您想跟踪图书馆中书的动态。您可能想跟踪每本书的以下属性:
202 0
|
存储 C# C++
C#基础知识 之 ✨ ref 和 out 之间的江湖趣闻
引用参数和输出参数 按照国际惯例,要了解一个东西的时候,首先明白它是什么,然后明白它能做什么,最后要知道为什么。 所以在介绍ref和out之前要先简单了解一下什么是引用参数与输出参数,因为使用ref和out要有所了解
C#基础知识 之 ✨ ref 和 out 之间的江湖趣闻