I have a class with a list property that seems to lose an element under certain circumstances. I cannot find out when this happens.
So what I'd like to do is set up a Visual Studio breakpoint that will pause the program the moment this value changes. A conditional breakpoint would not work in this scenario, since I have no idea what is removing this breakpoint.
To put it another way, I want my program to stop the moment myList.Count
evaluates to a new number.
Any ideas on how to do this?
This is not possible in C# or any of the other .NET languages due to CLR limitations. The Visual Studio native code debugger supports data breakpoints (link) for C++ code which do exactly this but this is not supported for managed code. You could try to break on or intercept Add
and Remove
method calls on the collection as suggested in the other answer to this question.
What about swapping out List<T>
for ObservableCollection<T>
and listen for the CollectionChanged
event? It implements the IList<T>
interface so there should be enough overlap in available methods to result in syntax and semantic compatibility.
This is now possible in Visual Studio 2019. See release notes here: https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes
This article goes into some detail using Preview 2. https://devblogs.microsoft.com/visualstudio/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/
Note that this is for .NET Core only and not the soon-to-be-legacy full fledged Windows only .NET framework.
I'm assuming Visual Studio is IDE.
Set a breakpoint, right click it, select condition, type myList.Count, and choose Has Changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With