[Import] Why does C #'s iterators feature spit out a class definition instead of a struct Definition

Source: Internet
Author: User
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 wocould 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 wowould have to use their types in the public protocol (using interfaces The structs wowould just get boxed ). that in turns means we 'd have to invent a name mangling scheme for the structs. in particle, iterators returning ienumerable <t> wocould be complicated because a type cocould have multiple such members that differ only in their parameter list, meaning that the parameter list wocould have to be part of the mangled name. (2) structs don't work in recursive cases. for example, A treenode type cocould 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 wowould 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 wowould therefore create a struct with a member of its own type .) you coshould 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 previusly non-recursive iterator changes its (supposedly private) implementation to become recursive. Anders (via Eric)


Source: http://blogs.msdn.com/csharpfaq/archive/2004/07/20/188865.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.