Can't view Dictionary contents in Visual Studio debugger
Asked Answered
B

1

11

I have a System.Collections.Generic.Dictionary object in my code and am trying to view its contents while stopped at a breakpoint in the Visual Studio debugger. The Dictionary class in .NET of course contains a list of keys and values.

If I right-click on the loaded object, and try to drill down into its contents, I seem to get into an infinite loop. For instance, if I'm trying to see the contained keys, I expand the Keys element, which shows me a count, and another collection called "Non-Public members". I expand the latter, and get another dictionary object, which has a Keys element, which I can expand to get another instance of "count" and "Non-Public members", which I can expand, etc., etc.:

Dictionary expansion in Visual Studio debugger

Using QuickWatch gives me the same result, so how do I actually view the keys contained in the object?

Berners answered 4/11, 2014 at 16:17 Comment(8)
Do you have a minimal example of code that generates this bug? Looking at a dictionary works fine for me in VS2010 using a dictionary to which I add a few items. Based on the screenshot you posted, nothing has been added to dctLoadedRows. Also, right clicking usually brings up a context menu and wouldn't let you drill down into anything.Isomer
@Isomer - The image is an actual screen shot (with the code to the left of the expansion clipped out.) The debugger is stopped at one of those lines in the code. In Visual Studio, you can expand an object from any reference in the code, as long as the object is still in scope. This is VS 2010. In this case it is a script task in SSIS. Right-clicking the object has brought up the dialog with the expansion button to the left of the "dctLoadedRows Count = 17", as you can see if you look at the top line of the expansion.Berners
Others have experienced this debugging Silverlight and WCF services in VS2010. Might you be in this situation or is this a "regular" VS app?: social.msdn.microsoft.com/Forums/vstudio/en-US/…Isomer
And just found this saying that the bug was fixed in VS 2010 SP1: #2790080Isomer
@Isomer - While it isn't Silverlight or WCF, as noted above it is part of a SQL Server Data Tools app. (SSIS script task.) So, maybe you're right about this being the same bug. However, I do seem to have SP1. Help -> About shows the version as "Version 10.0.40219.1 SP1 Rel".Berners
Sorry, I missed the part in your comment about SSIS. It probably didn't help that I didn't know what that meant either :) (My brain apparently has two acronym modes - decode ones it knows or can guess or ignore ones it doesn't).Isomer
not sure if it helps, but its a pain to work with dictionarys when debuging code, i usually create functions to dump their content to file, but its far from ideal.Acuminate
I got this on VS2019 on one computerHydrodynamic
D
6

I know this issue is fixed in later versions of Visual Studio. However, for some of us who are stuck in an older version of VS here is a quick fix to see the keys of a dictionary.

Let's say we have a dictionary named 'dict'. We need the keys to see the values. So in a watch window do this:

dict.Keys.ToList()

That will allow you to drill down into the list and see the keys.

If you know the index of the key you want to you do the following:

dict.Keys.ToList()[1]

This will display the key at index 1.

Now you can take that key and see what the value is with:

dict[dict.Keys.ToList()[1]]

Of course you can replace the index into the keys list with the actual key value in another watch line if that is easier.

EDIT: In addition, it is also possible to see the entries of a dictionary with the following in the watch window:

'dict.entries'

This will give you a list of entries to look through. Each entry will have a 'key' and 'value' property.

Demulsify answered 26/8, 2016 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.