调试foreach循环在C#中:什么迭代是什么? [英] Debugging a foreach loop in C#: what iteration is this?

查看:278
本文介绍了调试foreach循环在C#中:什么迭代是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了设置调试变量,每次启动的foreach时间递增它,当你打破了Visual Studio调试器连接,有没有办法告诉大家,这是通过循环第十届时间?

Other than setting a debug variable and incrementing it every time you start the foreach, when you break in with the Visual Studio debugger connected, is there a way to tell that this is the Xth time through the loop?

我想这将是Visual Studio中的一个功能,如果有的话,没有东西会被添加到编译code。

I guess this would be a feature of Visual Studio if anything, not something that would be added to the compiled code.

推荐答案

继承人previous堆栈&NBSP;溢出的问题,这似乎是你找什么: <一href="http://stackoverflow.com/questions/43021/c-get-index-of-current-foreach-iteration">get-index-of-current-foreach-iteration

Heres a previous Stack Overflow question that seems to be what your looking for: get-index-of-current-foreach-iteration

回答这个链接引用:

的Foreach是用于遍历实现IEnumerable的集合。   它通过对收集调用的GetEnumerator,这将   返回一个枚举器。

Foreach is for iterating over collections that implement IEnumerable. It does this by calling GetEnumerator on the collection, which will return an Enumerator.

这枚举有一个方法和属性:

This Enumerator has a method and a property:

      
  • 的MoveNext()
  •   
  • 电流
  •   

目前返回枚举器是目前上,MoveNext的对象   更新当前到下一个对象。

Current returns the object that Enumerator is currently on, MoveNext updates Current to the next object.

显然,索引的概念是外源的概念   枚举,且不能进行。

Obviously, the concept of an index is foreign to the concept of enumeration, and cannot be done.

正因为如此,多数藏品都能够使用遍历   索引和循环结构。

Because of that, most collections are able to be traversed using an indexer and the for loop construct.

我在这种情况下使用for循环相比,大大preFER   跟踪索引与本地变量

I greatly prefer using a for loop in this situation compared to tracking the index with a local variable.

这篇关于调试foreach循环在C#中:什么迭代是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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