Foreach扩展的想法 [英] Idea for a Foreach extension
问题描述
我不相信语法驱动的等价物存在于此,但我只是
认为如果你能用foreach做这样的事情会很好:
foreach(对象x在collection1,collection2中)
并让它按顺序枚举它们。
I don''t believe a syntax driven equivalent exists for this, but I just
thought it would be neat if you could use foreach to do something like this:
foreach (Object x in collection1, collection2)
and have it sequentially enumerate through both of them.
推荐答案
" Josh Ferguson" < _j *********** @ semo.edu>写在
新闻:eV ************** @ TK2MSFTNGP12.phx.gbl ...
"Josh Ferguson" <_j***********@semo.edu> wrote in
news:eV**************@TK2MSFTNGP12.phx.gbl...
我不相信一个语法驱动的等价物存在于此,但我只是认为如果你可以使用foreach做一些像
这样的东西会很好:
foreach(对象x在collection1中,集合2)
并按顺序枚举它们。
I don''t believe a syntax driven equivalent exists for this, but I just
thought it would be neat if you could use foreach to do something like
this:
foreach (Object x in collection1, collection2)
and have it sequentially enumerate through both of them.
我认为你可以做一些非常接近的事情:
foreach(新组合中的对象x(collection1,collection2))
其中组合是一个存储它在ctor中获取的列表的类,并且
实现IEnumerable来枚举它们。
无论如何,我认为有一个很好真正的语法支持,因为它
可以删除不必要的施法/拳击。
Niki
I think you can do something that comes quite close:
foreach (Object x in new Combine(collection1, collection2))
Where "Combine" is a class that stores the lists it gets in the ctor and
implements IEnumerable to enumerate over them.
Anyway, I think it would be nice to have real syntax support for this, as it
could remove unneccesary casting/boxing.
Niki
如果两个集合的长度不同,会发生什么。并且
你在哪里放置物品来自collection2你不能把它放进x
因为那是collection1的物品去的地方以及为什么停在两个
集合为什么不允许任意数量的集合。它可能看起来像一个很好的想法,但是它开始变得非常复杂,但是你预先允许的许多
集合中的某些人会想要1个更多
集合。
你可以使用while循环和集合迭代器。虽然如果收藏品的长度不同,还有什么问题呢?
我不确定我能想出任何理由无论如何你为什么要这么做。
我能想到的大多数事情都是设置类型操作,你会想要迭代其中一个集合无论如何,一次。
保罗。
Josh Ferguson < _j *********** @ semo.edu>在消息中写道
新闻:eV ************** @ TK2MSFTNGP12.phx.gbl ...
what happens if both of the collections are of different lengths though. and
where would you put the object from collection2 you can''t put it into x
because that is where the item from collection1 is going and why stop at two
collection why not allow any number of collections. It might look like a
nice idea but it starts to get horribly complicated, however many
collections you allow in foreach some one is going to want 1 more
collection.
You could use a while loop and the collection iterators. Although there is
still the problem of what to do if the collections are of differnt lengths.
I''m not sure I can think of any reason why you would want to do this anyway.
Most of the things I can think of would be set type operations and you would
only want to iterate through one of the collection at a time anyway.
Paul.
"Josh Ferguson" <_j***********@semo.edu> wrote in message
news:eV**************@TK2MSFTNGP12.phx.gbl...
我不是相信一个语法驱动的等价物存在于此,但我只是认为如果你可以使用foreach做一些像
这样的事情会很好:
foreach(collection1 in collection1,collection2)
并按顺序列举它们。
I don''t believe a syntax driven equivalent exists for this, but I just
thought it would be neat if you could use foreach to do something like this:
foreach (Object x in collection1, collection2)
and have it sequentially enumerate through both of them.
" Josh Ferguson" < _j *********** @ semo.edu>写道:
"Josh Ferguson" <_j***********@semo.edu> wrote:
foreach(collection1 in collection1,collection2)
并让它按顺序枚举它们。
foreach (Object x in collection1, collection2)
and have it sequentially enumerate through both of them.
听起来像嵌套循环的工作,除非由于某种原因你真的需要经常这样做。
foreach(收藏c in new Collection [] {collection1,
collection2})
{
foreach(c中的对象x)
{
P.
Sounds like a job for a nested loop, unless for some reason you really
need to do it very often.
foreach (Collection c in new Collection[] { collection1,
collection2 })
{
foreach (Object x in c)
{
P.
这篇关于Foreach扩展的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!