问题描述
我注意到名单,其中,T>
定义了它的枚举作为一个结构
,而的ArrayList
定义了它的枚举作为一个类
。有什么不同?如果我写一个枚举我的课,其中之一将是preferable?
I noticed that List<T>
defines its enumerator as a struct
, while ArrayList
defines its enumerator as a class
. What's the difference? If I am to write an enumerator for my class, which one would be preferable?
编辑:我的要求,不能用收益率
满足,所以我实现我自己的一个枚举。话虽如此,我不知道它是否会更好地遵循名单,其中的线条; T&GT;
并执行它作为一个结构
My requirements cannot be fulfilled using yield
, so I'm implementing an enumerator of my own. That said, I wonder whether it would be better to follow the lines of List<T>
and implement it as a struct.
推荐答案
像这种人,我会选择一个类。可变的结构是很讨厌。 (正如贾里德建议,我会使用迭代器块,手工编码枚举是繁琐得到正确的。)
Like this others, I would choose a class. Mutable structs are nasty. (And as Jared suggests, I'd use an iterator block. Hand-coding an enumerator is fiddly to get right.)
请参阅这个线程以列表枚举器是一个可变的结构引起的问题的一个例子...
See this thread for an example of the list enumerator being a mutable struct causing problems...
这篇关于枚举器实现:使用结构或类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!