vb.net:“foreach”中的索引号

发布于 2024-08-22 11:04:35 字数 381 浏览 4 评论 0原文

有时在 VB.net 中我有类似的情况:

For Each El in Collection
   Write(El)
Next

但如果我需要索引号,我必须将其更改为

For I = 0 To Collection.Count() - 1
   Write(I & " = " & Collection(I))
Next

甚至(更糟糕)

I = 0
For Each El In Collection
   Write(I & " = " & El)
   I += 1
Next

是否还有另一种获取索引的方法?

Sometime in VB.net i have something like:

For Each El in Collection
   Write(El)
Next

But if i need the index number, i have to change it to

For I = 0 To Collection.Count() - 1
   Write(I & " = " & Collection(I))
Next

Or even (worse)

I = 0
For Each El In Collection
   Write(I & " = " & El)
   I += 1
Next

Is there another way of getting the index?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤单情人 2024-08-29 11:04:35

如果您使用的是通用集合(Collection(of T)),那么您可以使用 IndexOf 方法

For Each El in Collection
   Write(Collection.IndexOf(El) & " = " & El)
Next

If you are using a generic collection (Collection(of T)) then you can use the IndexOf method.

For Each El in Collection
   Write(Collection.IndexOf(El) & " = " & El)
Next
冷…雨湿花 2024-08-29 11:04:35

我相信您使用计数器变量的原始方法是最有效的方法。使用 Linq 或 IndexOf 会降低性能。

Dim i as Integer = 0
For Each obj In myList
    'Do stuff
    i+=1
Next

I believe your original way of doing it with a counter variable is the most efficient way of doing it. Using Linq or IndexOf would kill the performance.

Dim i as Integer = 0
For Each obj In myList
    'Do stuff
    i+=1
Next
幽梦紫曦~ 2024-08-29 11:04:35

如果您需要索引,那么 for 循环是最直接的选择,并且具有出色的性能。除了您提到的替代方案之外,您还可以使用重载的 Select方法来跟踪索引并继续使用 foreach 循环。

Dim list = Enumerable.Range(1, 10).Reverse() ''# sample list
Dim query = list.Select(Function(item, index) _
                           New With { .Index = index, .Item = item })
For Each obj In query
    Console.WriteLine("Index: {0} -- Item: {1}", obj.Index, obj.Item)
Next

但是,如果唯一的原因是迭代它并知道索引,我会坚持使用 for 循环。上面并没有说明为什么你选择跳过 for 循环。

If you need the index then a for loop is the most straightforward option and has great performance. Apart from the alternatives you mentioned, you could use the overloaded Select method to keep track of the indices and continue using the foreach loop.

Dim list = Enumerable.Range(1, 10).Reverse() ''# sample list
Dim query = list.Select(Function(item, index) _
                           New With { .Index = index, .Item = item })
For Each obj In query
    Console.WriteLine("Index: {0} -- Item: {1}", obj.Index, obj.Item)
Next

However, I would stick to the for loop if the only reason is to iterate over it and know the index. The above doesn't make it clear why you chose to skip the for loop.

清风疏影 2024-08-29 11:04:35

如果您需要索引,请更改迭代集合的方式:

您已经想出了最简单的答案:

For i = 0 To Collection.Count() - 1
   DoStuffWith(Collection(i))
Next

If you need the index, change the way you iterate through the collection:

You have already come up with the simplest answer:

For i = 0 To Collection.Count() - 1
   DoStuffWith(Collection(i))
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文