普查员名单? [英] List of enumerators?
问题描述
我有各种各样的列表,我想以不同的速度进行操作,而且我想要通过一系列的普查员来控制它。
我有以下内容:
列表< List< DataEntry> mydata =新列表< List< Data>>();
文件清单<列表与LT;的DataEntry> .Enumeratormyenums =新闻
列表与LT;列表与lt;数据> .Enumertor>();
我
myenums.Add(mydata [0] .GetEnumerator());
then
myenums [0] .MoveNext();
<好吧,看起来好像不行......就像myenums [0]是临时副本
的调查员...我不知道但是什么时候我这样做,myenums [0]
实际上并没有移动。
e = myenums [0];
e .MoveNext();
虽然有效,但意思是e动作,但它并没有改变myenums [0]
(!??)
有人可以解释一下这里的理性和一个很好的方法来做我的工作吗?b $ b上午试图实现?
谢谢!
3月21日下午1点39分, AB" < a ... @ mytrashmail.comwrote:
我有各种各样的列表,我想以不同的速度和我一起点击
想用一个枚举器列表来控制它。
< snip>
请发一个简短但完整的程序来演示
问题。
请参阅 http:// pobox。 com / ~siget / csharp / complete.html 我的意思是
那个。
Jon
AB写道:
我有各种各样的列表,我想通过不同的节奏和我
想要用一个枚举器列表来控制它。
我有以下内容:
列表< List< DataEntry> mydata = new List< ;列表<数据>>();
列表与LT;列表与LT;的DataEntry> .Enumeratormyenums =新闻
列表与LT;列表与lt;数据> .Enumertor>(); <无线电通信/>
我做
myenums.Add(mydata [0] .GetEnumerator());
然后
myenums [0] .MoveNext();
看起来好像不行......就像myenums [0]是临时副本
$ b $调查员的b ...我不知道但是当我这样做时,myenums [0]
实际上并没有动。
e = myenums [0];
e.MoveNext();
虽然有效,但意思是e动作,但它并没有改变myenums [0]
(!??)
有人可以解释我的理性我和我想要实现的目标是什么?
谢谢!
你是什么正在体验的是值类型的使用。一个枚举器是一个结构,这意味着每当你从
列表中获得枚举器时,你就会得到它的副本。如果你想要更新
列表中的枚举器,你必须将更新后的枚举器放回到
列表中:
e = myenums [0];
e.MoveNext();
myenums [0] = e;
- -
G?ran Andersson
_____
http://www.guffa.com
这是一个显示发生了什么的工作代码
using System;
使用System.Collections.Generic;
使用System.Text;
命名空间ConsoleApplication1
{
class program
{
static void Main(string [] args)
{
列表与LT;列表与LT; INT→1 =新列表与LT;列表与LT; INT>>();
列表与LT;列表与LT; INT> .Enumeratore =新
List< List< int> .Enumerator>();
l.Add(new List< int>());
l.Add(new List< int>());
l [0]。添加(1);
l [0]。添加(2);
l [1]。添加(10);
l [1]。添加(11);
e.Add(l [0] .GetEnumerator());
e.Add( l [1] .GetEnumerator());
e [0] .MoveNext();
Console.WriteLine(e [0] .Current); //打印0,我想
打印" 1"
列表< int> .Enumerator f = e [1];
f.MoveNext(); f.MoveNext();
Console.WriteLine(" {0} {1}",f.Current,e [1] .Current); //
打印11 0,我想打印11 11
}
}
}
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!
On Mar 21, 1:39 pm, "A.B." <a...@mytrashmail.comwrote:I have various lists I want to go through, at different paces and I
want to control that with a list of enumerators.<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
A.B. wrote: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!
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
Here''s a working code showing what happens
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<List<int>l = new List<List<int>>();
List<List<int>.Enumeratore = new
List<List<int>.Enumerator>();
l.Add(new List<int>());
l.Add(new List<int>());
l[0].Add(1);
l[0].Add(2);
l[1].Add(10);
l[1].Add(11);
e.Add(l[0].GetEnumerator());
e.Add(l[1].GetEnumerator());
e[0].MoveNext();
Console.WriteLine(e[0].Current); // prints 0, I''d like to
print "1"
List<int>.Enumerator f = e[1];
f.MoveNext(); f.MoveNext();
Console.WriteLine("{0} {1}",f.Current,e[1].Current); //
prints 11 0, I''d like to print "11 11"
}
}
}
这篇关于普查员名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!