Unable to view dictionary contents in Visual Studio debugger

I have a System.Collections.Generic.Dictionary object in my code, and I'm trying to view its contents while stopping 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 a loaded object and try to expand its contents, I seem to end up in an endless loop. For example, if I try to see the contained keys, I expand the Keys element, which shows me the score, and another collection is called "Non-Public members". I extend the latter and get another dictionary object that has a Keys element that I can expand to get another instance of "count" and "Non-Public members" that I can extend, etc. Etc.:

Dictionary expansion in Visual Studio debugger

Using QuickWatch gives me the same result, since I'm really looking at the keys contained in an object?

+5
source share
1 answer

I know this problem has been fixed in later versions of Visual Studio. However, for some of us that are stuck in the old version of VS, this is a quick fix to see dictionary keys.

Say we have a dictionary called 'dict'. We need keys to see the meanings. Therefore, in the clock window, do the following:

dict.Keys.ToList()

This will allow you to turn around in the list and see the keys.

If you know the index of the key you want to make, follow these steps:

dict.Keys.ToList()[1]

This will display the key in index 1.

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

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

Of course, you can replace the index with a list of keys with the actual key value in another viewing line, if that is easier.

EDIT: In addition, in the viewport you can also see dictionary entries with the following:

'dict.entries'

This will give you a list of entries to view. Each entry will have a property of "key" and "value".

+2
source

Source: https://habr.com/ru/post/1206180/


All Articles