foreach&数组列表 [英] foreach & arraylist

查看:60
本文介绍了foreach&数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们是否有任何想法,为什么你们不能修改一个阵列

列表,而不是在它的元素中。


我写了自己的收藏品,我正在尝试添加相同的行为,但是我不想知道从哪里开始。


我虽然内部事件,但忽略了这个想法,因为它可以防止垃圾收集整个枚举器。

任何其他想法?

解决方案



" Lloyd Dupont" < net.galador@ld>在消息中写道

新闻:ea ************** @ TK2MSFTNGP12.phx.gbl ...

你们有没有人最简单的想法为什么你不能在元素的元素中修改数组
列表。

因为这是关于枚举器的规则,它使得枚举器

实现很简单。允许更改需要更复杂,

线程安全的枚举器。在这种情况下,一个错误的可能性非常大。我写了自己的收藏品,我正在尝试添加相同的行为,但我不知道从哪里开始。

我虽然对内部事件却反对这个想法,因为它防止枚举器的
垃圾收集。

嗯,简单的方法,坚持像int版本的私有字段;在你的班级

并按照你的正确锁定规则在每次更改时增加它,当然还有

方法(你可能需要使用

系统.Threading.Interlocked.Increment在这种情况下,假设多个

线程,枚举器和处理器)。然后在

枚举器的每个命令上检查该值,如果它在创建枚举器后发生了变化,

您的枚举器无效并且应该抛出

IEnumerable documentation。





" Lloyd Dupont" < net.galador@ld>在消息中写道

新闻:ea ************** @ TK2MSFTNGP12.phx.gbl ...

你们有没有人最简单的想法,为什么你不能修改一个阵列
列表,而在它的元素的foreach。

我写了自己的集合,我正在尝试添加相同的行为但是我不知道从哪里开始。

我虽然对内部事件却反对这个想法,因为它阻止了枚举器的
垃圾收集。
任何其他想法?

我忘了提及,你可能会使用内部事件。 foreach将

调用IDisposable.Dispose如果枚举器实现IDisposable,所以你

可以使用该调用来清理,它不是很干净但它可以工作。


in的用法关键字不允许你修改

arraylist的内容。


就像int,bool这样的值类型而言,它们可能是

可修改。


但是当对象添加到arraylist时,in关键字在forach,

返回一个只读参考,你不能指向其他

参考


一个不能改变in的行为,因为它是一个关键字&不是运营商

例如

foreach(人员代表)

p =新人(我的名字); //这个不能完成,因为p是

这里只读(正如编译器所说)


但你可以做以下


foreach(al pist中的人物)

p.name =" new name";


如果你想存储(al i = 0; i< alist.Count; i ++)

alist [i] =新人(新名称) )//现在可以这样做,因为它是

使用下标操作符,它返回一个对象实例


我希望我回答,你想要什么。如果有什么不清楚,请写回

小组

虽然我不是专家,但我的理解告诉我实现它的方法

HTH

Kalpesh

" Lloyd Dupont" < net.galador@ld>在消息中写道

新闻:ea ************** @ TK2MSFTNGP12.phx.gbl ...

你们有没有人最简单的想法,为什么你不能修改一个阵列
列表,而在它的元素的foreach。

我写了自己的集合,我正在尝试添加相同的行为但是我不知道从哪里开始。

我虽然对内部事件却反对这个想法,因为它阻止了枚举器的
垃圾收集。
任何其他想法?





Does any of you have the slightest ideas of why you can''t modify an array
list while in a foreach of its element.

I wrote my own collection and I''m trying to add the same behavior but I
don''t know where to start.

I though to internal events but dismiss the idea because it prevents garbage
collection of the enumerator.
any other ideas ?

解决方案


"Lloyd Dupont" <net.galador@ld> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...

Does any of you have the slightest ideas of why you can''t modify an array
list while in a foreach of its element.
Because that is the rules when it comes to enumerators, it makes enumerator
implementation alot easier. Allowing changes requires a more sophisticated,
thread-safe enumerator. The probability of a bug in that case would be very
large in deed. I wrote my own collection and I''m trying to add the same behavior but I
don''t know where to start.

I though to internal events but dismiss the idea because it prevents garbage collection of the enumerator.
any other ideas ?
Well, simple method, stick a private field like int version; in your class
and increment it on every change, following your proper locking rules and
methodologies of course(you may need to use
System.Threading.Interlocked.Increment in this case, assuming multiple
threads, enumerators, and processors). Then on every command to the
enumerator check that value, if it changed since the enumerator was created,
your enumerator is invalid and should throw an exception as specified in the
IEnumerable documentation.




"Lloyd Dupont" <net.galador@ld> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...

Does any of you have the slightest ideas of why you can''t modify an array
list while in a foreach of its element.

I wrote my own collection and I''m trying to add the same behavior but I
don''t know where to start.

I though to internal events but dismiss the idea because it prevents garbage collection of the enumerator.
any other ideas ?
I forgot to mention, you could probably use internal events. foreach will
call IDisposable.Dispose if the enumerator implements IDisposable, so you
could use that call to clean up, its not as clean but it would work.



The usage of "in" keyword doesnt let you modify the content of the
arraylist.

As far as valuetype such as int,bool are concerned, they might be
modifiable.

But when objects are added to the arraylist, the "in" keyword in forach,
returns a read-only reference, which you cant make it point to other
reference

One cant change the behaviour of "in", since its a keyword & not an operator
for eg
foreach (person p in alist)
p = new person("my name"); // this cant be done, because p is
read-only here (as the compiler says)

but you can do the following

foreach (person p in alist)
p.name = "new name";

If you want to store a new reference in alist
for(int i =0; i < alist.Count; i++)
alist[i] = new person("new name") // now this can be done, since it is
using subscript operator, which returns an object instance

I hope I answered, what you wanted. If anything is unclear, do write back on
the group
I am no expert though, but my understanding tells me this way to achieve it

HTH
Kalpesh
"Lloyd Dupont" <net.galador@ld> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...

Does any of you have the slightest ideas of why you can''t modify an array
list while in a foreach of its element.

I wrote my own collection and I''m trying to add the same behavior but I
don''t know where to start.

I though to internal events but dismiss the idea because it prevents garbage collection of the enumerator.
any other ideas ?





这篇关于foreach&amp;数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆