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

10

我正在使用一个IEnumerable,但在调试器中我希望能看到它所包含的项,但是因为没有任何属性或items,我无法做到。

有可能查看IEnumerable中包含的项吗?


将鼠标悬停在该变量上,展开ResultView? - nhabuiduc
6个回答

8

按照以下指示操作:

enter image description here

即点击被突出显示区域左侧的那些圆形箭头。


7
使用“本地”窗口并展开结果视图。

5

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

调试 -> 窗口 -> 快速窗口,然后在快速窗口中输入以下命令:

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

本地窗口很好,但我喜欢这个答案,因为当您使用即时窗口时,窗口会在调试会话后保留您的结果。 - secretwep

3
作为扩展结果视图的替代方法,您可以在监视窗口中使用“结果”格式说明符

例如,如果可枚举变量名为“myIEnumerable”,则在监视窗口的名称列中键入“myIEnumerable,results”。
与扩展结果视图相比,结果说明符的轻微优点在于它避免显示各种枚举器属性,从而为结果保留屏幕空间。

2
有许多可能性与此相关,你可以尝试。
 .ToList();
 // and access single elements through the index
 .ToList()[0];
 //you can use type those commands in the table at
 //"observe local"

use "observe local" in VS


1
在Visual Studio 2013社区版中,调试器根据有效数据类型显示结果集:

enter image description here

无论您悬停在接口类型的变量还是原始集合变量上,都没有区别。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接