[导入]Why does C#'s iterators feature spit out a class definition instead of a struct definition?
2004-09-22 09:34
337 查看
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://blogs.msdn.com/csharpfaq/archive/2004/07/20/188865.aspx
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://blogs.msdn.com/csharpfaq/archive/2004/07/20/188865.aspx
相关文章推荐
- Why does MariaDB 10.2 use InnoDB instead of XtraDB?
- Why does Quora use MySQL as the data store instead of NoSQLs such as Cassandra, MongoDB, or CouchDB?
- C# 操作MongoDb 错误Element '_v' does not match any field or property of class XXX
- [导入]Struct 和Class的区别,在C#中
- Q&A: C# Language 2.0--(Why don't nullable relational operators return bool? instead of bool?)
- Why does Delphi XE7 IDE hangs and fails on out of memory exception?
- Why does Quora use MySQL as the data store instead of NoSQLs such as Cassandra, MongoDB, CouchDB etc?
- how does Interface Wrapper promote re use / ease of use of interface class
- The serializable class XXX does not declare a static final serialVersionUID field of type long的警告
- storm杂谈之Why use netty as transport instead of zeromq
- C# class和struct的区别和用法
- error C2079: “out”使用未定义的 class“std::basic_ofstream<_Elem,_Traits>”
- C#使用ManagementObjectSearcher来获取系统信息时会有out of memory的异常
- c#中struct和class的区别[转]
- 关于问题The fully qualified name of the bean's class, except if it serves only as a parent definition fo
- The serializable class Myplayer does not declare a static final serialVersionUID field of type long
- c#中,struct和class的区别
- C# class and struct
- C# Class and Struct 区别
- c#中struct和class的区别