问题描述
我有各种各样的列表,我想以不同的速度进行操作,而且我想要通过一系列的普查员来控制它。
我有以下内容:
列表< List< DataEntry> mydata =新列表< List< Data>>();
文件清单<列表与LT;的DataEntry> .Enumeratormyenums =新闻
列表与LT;列表与lt;数据> .Enumertor>();
我
myenums.Add(mydata [0] .GetEnumerator());
then
myenums [0] .MoveNext();
的调查员...我不知道但是什么时候我这样做,myenums [0]
实际上并没有移动。
e = myenums [0];
e .MoveNext();
虽然有效,但意思是e动作,但它并没有改变myenums [0]
(!??)
有人可以解释一下这里的理性和一个很好的方法来做我的工作吗?b $ b上午试图实现?
谢谢!
< snip>
请发一个简短但完整的程序来演示
问题。
请参阅我的意思是
那个。
Jon
你是什么正在体验的是值类型的使用。一个枚举器是一个结构,这意味着每当你从
列表中获得枚举器时,你就会得到它的副本。如果你想要更新
列表中的枚举器,你必须将更新后的枚举器放回到
列表中:
e = myenums [0];
e.MoveNext();
myenums [0] = e;
- -
G?ran Andersson
_____
Hi,
I have various lists I want to go through, at different paces and I
want to control that with a list of enumerators.
I have the following:
List<List<DataEntry>mydata = new List<List<Data>>();
List<List<DataEntry>.Enumeratormyenums = news
List<List<Data>.Enumertor>();
I do
myenums.Add(mydata[0].GetEnumerator());
then
myenums[0].MoveNext();
well it seems that doesn''t work... like myenums[0] is a temporary copy
of the enumerator... I don''t know but when I do that, myenums[0]
desn''t actually move.
e = myenums[0];
e.MoveNext();
works though, meaning e moves, but it doesn''t change myenums[0]
( !?? )
Can someone explain me the rational here and a good way to do what I
am trying to achieve?
Thanks!
<snip>
Please post a short but complete program that demonstrates the
problem.
See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.
Jon
What you are experiencing is the usage of a value type. An enumerator is
a structure, which means that whenever you get the enumerator from the
list, you are getting a copy of it. If you want the enumerator in the
list to be updated, you have to put the updated enumerator back into the
list:
e = myenums[0];
e.MoveNext();
myenums[0] = e;
--
G?ran Andersson
_____
http://www.guffa.com
这篇关于普查员名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!