在C#3中的Forevery()可写迭代器机制 [英] Forevery() writable iterator mechanism in C# 3

查看:57
本文介绍了在C#3中的Forevery()可写迭代器机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了网络,看看其他开发人员是否一直在C#中寻找一个可写的迭代器。我发现了很多讨论,因此发布了这篇文章。


目前(C#2)你不能将ref和out参数传递给迭代器

方法(一个返回IEnumerable )。我想对一个集合上的变形操作进行这样的操作。


我意识到需要锁定目标等等。

>
有没有人知道如何在C#2中处理''可写迭代器'?


是否有人知道C#的功能请求提供带有<的forevery() br />
匹配的界面如IEnumerable?


提前谢谢。我想提出一个产品功能请求但是想要b / b
事先做一些研究。

I searched the net to see if other developers have been looking for a
writable iterator in C#. I found much discussion and thus this post.

Currently (C# 2) you can not pass ref and out arguments to an iterator
method (one returning IEnumerable). I WOULD like to do this for
transformative operations on a collection.

I realize the need to lock target, etc.

Does anyone know how to handle ''writable iterators'' in C# 2?

Is anyone aware of feature requests for C# to provide a forevery() with a
matching interface like IEnumerable?

Thanks ahead of time. I want to put in a product feature request but want to
do some research before hand.

推荐答案

shawnk< Sh****@discussions.microsoft.com>写道:
shawnk <Sh****@discussions.microsoft.com> wrote:
我搜索网络,看看其他开发人员是否一直在寻找C#中的可写迭代器。我找到了很多讨论,因此发布了这篇文章。

目前(C#2)你不能将ref和out参数传递给迭代器
方法(一个返回IEnumerable)。我想在集合上进行变形操作时这样做。

我意识到需要锁定目标等等。

有谁知道如何处理' C#2中的可写迭代器?

是否有人知道C#的功能请求提供了一个带有IEnumerable匹配接口的forevery()?

提前谢谢。我想提出一个产品功能请求但是想要事先做一些研究。
I searched the net to see if other developers have been looking for a
writable iterator in C#. I found much discussion and thus this post.

Currently (C# 2) you can not pass ref and out arguments to an iterator
method (one returning IEnumerable). I WOULD like to do this for
transformative operations on a collection.

I realize the need to lock target, etc.

Does anyone know how to handle ''writable iterators'' in C# 2?

Is anyone aware of feature requests for C# to provide a forevery() with a
matching interface like IEnumerable?

Thanks ahead of time. I want to put in a product feature request but want to
do some research before hand.




我不太清楚你想要什么。你能给一个你喜欢*工作的样品吗?


-

Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet 博客: http://www.msmvps。 com / jon.skeet

如果回复小组,请不要给我发邮件



I''m not entirely clear about what you want. Could you give a sample
program that you''d *like* to work?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too




" shawnk" <嘘**** @ discussions.microsoft.com>在消息中写道

新闻:B5 ********************************** @ microsof t.com ...

"shawnk" <Sh****@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
我在网上搜索了一下其他开发人员是否一直在寻找C#中的可写迭代器。我找到了很多讨论,因此发布了这篇文章。

目前(C#2)你不能将ref和out参数传递给迭代器
方法(一个返回IEnumerable)。我想在集合上进行变形操作时这样做。

我意识到需要锁定目标等等。

有谁知道如何处理' C#2中的'可写迭代器'?
是否有人知道C#的功能请求提供了一个带有IEnumerable匹配接口的forevery()?

提前感谢。我想提出一个产品功能请求,但希望
能够事先做一些研究。
I searched the net to see if other developers have been looking for a
writable iterator in C#. I found much discussion and thus this post.

Currently (C# 2) you can not pass ref and out arguments to an iterator
method (one returning IEnumerable). I WOULD like to do this for
transformative operations on a collection.

I realize the need to lock target, etc.

Does anyone know how to handle ''writable iterators'' in C# 2?
Is anyone aware of feature requests for C# to provide a forevery() with a
matching interface like IEnumerable?

Thanks ahead of time. I want to put in a product feature request but want
to
do some research before hand.




为什么界面需要ref / out本身?

没有什么能阻止屈服功能从破坏性地修改它的集合而不管它返回什么,是吗? (虽然

但肯定不常见或预期。)

根据定义,破坏性操作将在所有参考文献中被重新引用

收集,所以外出不需要。

如果它不是破坏性的,你可以写一点收集。包装器

'会通过枚举并返回一个新的ICollection。


恕我直言,如果你是:

- 制作复制列表(非破坏性)或返回每个元素,然后

使用返回值

- 如果您正在执行破坏性操作,请使fnvoid ;并且

不返回值 - 这清楚地表明发生了什么


如果所有类型都是引用类型(类实例),那么每个从枚举器返回的元素是一个共享的,可变的对象 - 所以你总是可以通过正常方式改变它。 (这不是C ++,类实例不是通过堆栈上的值传递的
)如果你想改变存储的,那么元素

本身是来自enumertor函数的* outside *

的底层集合中的当前迭代,这是另一个故事 - 但那真的很糟糕

练习 - 特别是考虑到可能根本没有实际的基础

集合 - 这确实是收益率的一个主要点,提供

此类事项的参考透明度。考虑:

收益率收益1;

收益率收益率2;

收益率收益率3;


你不能写不管你是什么,你都会这个交互者。


一种方法是公开一个返回代理的迭代器,用于

每个元素的存储,如果实际上存在

枚举的后备存储。例如:


//当然,泛型也是......

interface IEnumStorageProxy {object Value {get;组;
interface IEnumHasStorage {GetStorageProxyEnum(); }


foreach(对象elem in coll)

elem.Change(blah); //像往常一样在集合中更改对象


foreach(在coll.GetStorageProxyEnum()中的IEnumStorageProxy单元格)

{

object oldVal = c.Value;

c.Value = new MyObject(); //替换集合中的对象

}


这不需要新的语言功能,但接口需要

从框架集中暴露出来。


m



Why would the interface need ref/out itself?
There''s nothing stopping the "yield"ing function from destructively
modifying it''s collection regardless of what it returns, is there? (Although
it''s certainly not common or expected.)
A destructive operation will by definition be refelected in all references
to the collection, so "out" is not needed.
And if it''s not destructive, you can just write a little "collect" wrapper
that''ll go though the enum and return a new ICollection.

IMHO, if you are:
- making a copied list (non-destructive) or returning each element, then
use a return value
- if you are performing a destructive operation, make the fn "void" and
do not return a value - this make it clear what''s going on

If all your types are reference types (class instances), then each returned
element from the enumerator is a shared, mutable, object - so you can always
change it through normal means. (This isn''t C++, class instances aren''t
passed by value on the stack) If you want to change the "stored" element
itself of the current iteration in the underlying collection from *outside*
of the enumertor function, that''s another story -- but that''s really bad
practice -- especially considering that there may be no actual underlying
collection at all - that''s really a major point of "yield", to provide
referential transparency in such matters. Consider:
yield return 1;
yield return 2;
yield return 3;

Your not going to be able to "write" to this interator no matter what you
do.

One way to do this would be to expose an iterator that returned proxies for
the storage of each element, if in fact there is a backing-store for the
enumeration. For example:

// Of course, generics too...
interface IEnumStorageProxy { object Value { get; set; }};
interface IEnumHasStorage { GetStorageProxyEnum(); }

foreach ( object elem in coll)
elem.Change(blah); // changing object inside collection as usual

foreach ( IEnumStorageProxy cell in coll.GetStorageProxyEnum())
{
object oldVal = c.Value;
c.Value = new MyObject(); // replace the object in the collection
}

This requires no new language features, though the interfaces would need to
be exposed from the framework collections.

m




错别字...

Typos...
interface IEnumHasStorage {GetStorageProxyEnum(); }
应该是:interface IEnumHasStorage {IEnumeration GetStorageProxyEnum(); }
object oldVal = c.Value;
c.Value = new MyObject(); //替换集合中的对象
应该是:object oldVal = cell.Value;
cell.Value = new MyObject(); //替换
集合中的对象
interface IEnumHasStorage { GetStorageProxyEnum(); } Should be: interface IEnumHasStorage { IEnumeration GetStorageProxyEnum(); } object oldVal = c.Value;
c.Value = new MyObject(); // replace the object in the collection Should be: object oldVal = cell.Value;
cell.Value = new MyObject(); // replace the object in the
collection



但是我确定你有这个想法...

我我不是说接口/方法的名字也很好:)



but I''m sure you got the idea...
I''m not saying the names of the interfaces/methods are so nice either :)


这篇关于在C#3中的Forevery()可写迭代器机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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