调试时如何查看IEnumerable的元素?

How to see the elements of IEnumerable while debugging?

我正在使用 IEnumerable,在调试器中我想查看它所包含的项目,但我看不到,因为 属性 这两个项目都没有。

是否可以查看具有 IEnumerable 的项目?

通过执行此指示您执行的操作:

即单击突出显示区域左侧的圆形箭头。

使用 "local" window 并展开结果视图。

Visual Studio 2013 Community Edition中,调试器根据有效数据类型显示结果集:

将鼠标悬停在接口类型变量或原始集合变量上没有区别。

这有很多种可能性。 你可以试试

 .ToList();
 // and access single elements through the index
 .ToList()[0];
 //you can use type those commands in the table at
 //"observe local"

作为扩展结果视图的替代方法,您可以在手表 window 中使用 "results" format specifier.

例如,如果可枚举变量命名为"myIEnumerable",则在手表window的名称栏中键入"myIEnumerable, results"。

结果说明符相对于扩展结果视图的次要好处是它避免显示各种枚举器属性,从而保留结果的屏幕空间。

调试时,设置一个断点然后在VS中转到:

Debug -> Windows -> Immediate 然后在立即 window 中键入这些命令:

yourEnumerable.ToList()
yourEnumerable.Count() // see how many elements are in
yourEnumerable.ToList()[0] // access by index
yourEnumerable.ToArray()[0] // also access by index