vb.net:索引号在“for each”

前端之家收集整理的这篇文章主要介绍了vb.net:索引号在“for each”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有时在VB.net我有一些像:
  1. For Each El in Collection
  2. Write(El)
  3. Next

但是,如果我需要索引号,我必须改变它

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

甚至(更糟)

  1. I = 0
  2. For Each El In Collection
  3. Write(I & " = " & El)
  4. I += 1
  5. Next

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

如果您使用通用集合(Collection(of T)),则可以使用 IndexOf method.
  1. For Each El in Collection
  2. Write(Collection.IndexOf(El) & " = " & El)
  3. Next

猜你在找的VB相关文章