何时使用For Each ... Next vs For ... Next [英] When to use For Each...Next vs For...Next

查看:54
本文介绍了何时使用For Each ... Next vs For ... Next的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的两篇文本/参考书中说,如果可能的话,最好使用For Each ... Next循环,而不是For ... Next循环。我正在使用下面显示的代码。它工作正常。代码所做的是将部分数据行(第1列到第4列和第6列)从一个dgv1控件移动到另一个dgv2控件,其中第1列必须满足If条件。我的问题是,如果它使用For Each构造,这段代码将如何显示。



Two of my Text/Reference books say that it is better to use For Each...Next loops if possible as opposed to For...Next loops. I am using the code shown below. It works fine. What the code does is move partial rows of data (columns 1 through 4 and column 6) from one dgv1 control to another dgv2 control, where column 1 must meet the If criteria. My question is, how would this code look if it used a For Each construct.

For Me.g = 0 To dgv1.RowCount - 1
            If dgv1(0, g).Value = txtClass.Text Then
                dgv2.Rows.Add()
                For Me.h = 1 To 4
                    dgv2(h, j).Value = dgv1(h, g).Value
                Next
                dgv2(6, j).Value = dgv1(6, g).Value
                j = j + 1
            End If
        Next

推荐答案

请阅读这篇文章:

FOREACH Vs. FOR(C#) [ ^ ]

In。 NET,哪个循环运行得更快,'for'或'foreach'? [ ^ ]



Please, read this article:
FOREACH Vs. FOR (C#)[^]
In .NET, which loop runs faster, 'for' or 'foreach'?[^]

会员10628309问:
Member 10628309 asked:

如果使用For Each构造,该代码将如何显示。



how would this code look if it used a For Each construct.

For Me.g = 0 To dgv1.RowCount - 1
            If dgv1(0, g).Value = txtClass.Text Then
                dgv2.Rows.Add()
                For Me.h = 1 To 4
                    dgv2(h, j).Value = dgv1(h, g).Value
                Next
                dgv2(6, j).Value = dgv1(6, g).Value
                j = j + 1
            End If
        Next





例如:



for example:

For Each r As DataGridViewRow In DataGridViewRowCollection
    'for loop body
Next 





如需了解更多信息,请参阅:

For Each ...下一个声明 [ ^ ]

DataGridViewRow [ ^ ]

DataGridViewCollection [ ^ ]



For further information, please see:
For Each... Next statement[^]
DataGridViewRow[^]
DataGridViewCollection[^]


Th很简单。如果在每次迭代中都需要元素的索引,请使用for。如果您需要保证某些操作顺序,请使用for。在所有其他情况下,尝试使用foreach。



一个微妙的情况是当你需要修改你在迭代期间迭代的集合时,特别是删除一些元素,一种方法是:使用for迭代来自高指数到低指数。对这种情况要非常小心。其他更复杂的情况需要特别考虑。在这种情况下,只需使用你的逻辑。



请记住,foreach的确切行为取决于接口 System.IEnumerable ,它的 System.Collections.IEnumerator 的实现如何表现。请参阅:

https ://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28v=vs.110%29.aspx [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator(v=vs.110).aspx> https://msdn.microsoft.com/en- us / library / system.collections.ienumerable.getenumerator(v = vs.110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.collections.ienumerator% 28v = vs.110%29.aspx [ ^ ]。



首先,明智的做法是不依赖于此接口的特定实现中已知的任何特殊功能。另一方面,您可以以特定的方式实现此接口,以使一些额外的假设有效,但我不推荐它。



如果你想说:这个答案几乎说'它取决于',没有别的,这基本上就是它的意思。 :-)







我后来意识到我的答案没有解决更具体的问题这个问题的问题。所以,请参阅解决方案2和我对它的评论。



-SA
This is simple enough. If in each iteration you need index of the element, use "for". If you need to guarantee certain order of operations, use "for". In all other cases, try to use "foreach".

One delicate case is when you need to modify the set you are iterating during iterations, in particular, remove some elements, one approach is: using "for" iterating from high index to low index. Be very careful with such cases. Other, more complicated cases, need special consideration. In such cases, just use you logic.

Remember that exact behavior of "foreach" depends on how the interface System.IEnumerable is implemented, how its implementation of System.Collections.IEnumerator behaves. Please see:
https://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.collections.ienumerator%28v=vs.110%29.aspx[^].

For one thing, it would be wise not to rely on any peculiar features known from a particular implementation of this interface. From the other hand, you can implement this interface in a specific way, to make some extra assumptions valid, but I would not recommend it.

If you want to say: "this answer pretty much says 'it depends', nothing else", this is essentially what it meant to be. :-)



I later realized that my answer did not address some more specific problems of the question. So, please see Solution 2 and my comment to it.

—SA


我认为 For .. Next 循环可以很好地解决您的代码问题。

但是,即使我的生活依赖于它,我也不会将成员变量用作循环索引。
I think your code is well addressed by the For .. Next loop.
However, I wouldn't have used member variables as loop indices even if my life depended on it.


这篇关于何时使用For Each ... Next vs For ... Next的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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