{"id":223583,"date":"2019-02-12T09:00:38","date_gmt":"2019-02-12T17:00:38","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=223583"},"modified":"2019-02-14T15:23:09","modified_gmt":"2019-02-14T23:23:09","slug":"break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019\/","title":{"rendered":"Break When Value Changes: Data Breakpoints for .NET Core in Visual Studio 2019"},"content":{"rendered":"<p><em>\u201cWhy is this value changing unexpectedly and where or when is this occurring?!<\/em>\u201d<\/p>\n<p>This is a question many of us dread asking ourselves, knowing that we&#8217;ll have to do some tedious trial-and-error debugging\u00a0 to locate the source of this issue.\u00a0 For C++ developers, the exclusive solution to this problem has been the <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2018\/07\/25\/data-breakpoints-15-8-update\/\">data breakpoint<\/a>, a debugging tool allowing you to break when a specific object\u2019s property changes.\u00a0 Fortunately, data breakpoints are no longer a C++ exclusive because they are now available for\u00a0<a href=\"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2019\/01\/29\/announcing-net-core-3-preview-2\/\">.NET Core (3.0 or higher)<\/a> in Visual Studio 2019 Preview 2!<\/p>\n<p>Data breakpoints for managed code were a <a href=\"https:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio-ide\/suggestions\/6097301-support-data-breakpoints-for-c\">long-requested ask<\/a> for many of you. They are a great alternative to simply placing a breakpoint on a property\u2019s setter because a data breakpoint focuses on a <em>specific <\/em>object\u2019s property even when it\u2019s out of scope, whereas the former option may result in constant, irrelevant breaks if you have hundreds of objects calling that function.<\/p>\n<h3>How do I set a data breakpoint?<\/h3>\n<p>Setting a data breakpoint is as easy as right-clicking on the property you\u2019re interested in watching inside the watch, autos, or locals window and selecting \u201cBreak when value changes\u201d in the context menu.\u00a0 All data breakpoints are displayed in the Breakpoints window. They are also represented by the standard, red breakpoint circle next to the specified property.<\/p>\n<p><figure id=\"attachment_223578\" aria-labelledby=\"figcaption_attachment_223578\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-223578 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/02\/ManagedDataBreakpointSet.gif\" alt=\"Setting a data breakpoint\" width=\"1208\" height=\"791\" \/><figcaption id=\"figcaption_attachment_223578\" class=\"wp-caption-text\">Setting a data breakpoint in the Locals window and viewing the breakpoint in the Breakpoints window<\/figcaption><\/figure><\/p>\n<h3>When can I use data breakpoints?<\/h3>\n<p>Now that you know how to set a data breakpoint, now what?\u00a0 Here are some ways to take advantage of data breakpoints when debugging your .NET Core applications.<\/p>\n<p>Let\u2019s say that you want to figure out who is modifying a property in an object and for most of the time, this property change does not happen in the same file. By setting a data breakpoint on the property of interest and continuing, the data breakpoint will stop at the line after the property has been modified.<\/p>\n<p><figure id=\"attachment_223579\" aria-labelledby=\"figcaption_attachment_223579\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-223579 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/02\/ManagedDataBreakpointBreakWhenValueChanges.gif\" alt=\"Break when value changes\" width=\"1208\" height=\"791\" \/><figcaption id=\"figcaption_attachment_223579\" class=\"wp-caption-text\">Break when _data value changes<\/figcaption><\/figure><\/p>\n<p>This also works for objects. The data breakpoint will stop when the property referencing the object changes value, not when the contents of the object change.<\/p>\n<p><figure id=\"attachment_223580\" aria-labelledby=\"figcaption_attachment_223580\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-223580 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/02\/ManagedDataBreakpointBreakWhenValueChangesObject.gif\" alt=\"Break when object reference changes\" width=\"1208\" height=\"791\" \/><figcaption id=\"figcaption_attachment_223580\" class=\"wp-caption-text\">Break when the property referencing an object changes<\/figcaption><\/figure><\/p>\n<p>As illustrated in the GIF above, calling the toEdit._artist.ChangeName() function did not cause a breakpoint to hit since it was modifying a property (Name) inside the Song&#8217;s Artist property.\u00a0 In contrast, the data breakpoint hits when the _artist property is assigned a reference to a new object.<\/p>\n<p>Data breakpoints are also useful when you want to know when something is added or removed from a collection. Setting a data breakpoint on the \u2018Count\u2019 field of classes from System.Collections.Generic makes it easy to detect when the collection has changed.<\/p>\n<p><figure id=\"attachment_223581\" aria-labelledby=\"figcaption_attachment_223581\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-223581 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2019\/02\/ManagedDataBreakpointSetOnListCount.gif\" alt=\"Break when item is added or removed from list\" width=\"1208\" height=\"791\" \/><figcaption id=\"figcaption_attachment_223581\" class=\"wp-caption-text\">Break when an object is added or removed from a list<strong style=\"font-size: 1rem\">\u00a0<\/strong><\/figcaption><\/figure><\/p>\n<h3>Are there opportunities for improving managed data breakpoints?<\/h3>\n<p>Since Visual Studio 2019 is still in preview, we highly encourage you to experiment, play around with, and provide feedback for this iteration of data breakpoints.\u00a0 Here are some known scenarios where data breakpoints currently cannot be set that we are working on erasing and improving in future Visual Studio updates:<\/p>\n<ul>\n<li>Properties that are not expandable in the tooltip, Locals, Autos, or Watch window<\/li>\n<li>Static variables<\/li>\n<li>Classes with the DebuggerTypeProxy Attribute<\/li>\n<li>Fields inside of structs<\/li>\n<\/ul>\n<p>Managed data breakpoints also exclude properties that call native code and properties that depend on too many fields.<\/p>\n<h3>Ready to try data breakpoints in your .NET Core applications?\u00a0 Let us know in the comments!<\/h3>\n<p>For any issues or suggestions about this feature, please let us know via Help &gt; Send Feedback &gt; <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio-2017\">Report a Problem<\/a> in the IDE or in the <a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/8\/index.html\">Developer Community<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u201cWhy is this value changing unexpectedly and where or when is this occurring?!\u201d This is a question many of us dread asking ourselves, knowing that we&#8217;ll have to do some tedious trial-and-error debugging attempting to locate the source of this issue.\u00a0 For C++ developers, the exclusive solution to this problem has been the data breakpoint, a debugging tool that allows you to break when a specific object\u2019s property changes.\u00a0 Fortunately, data breakpoints are no longer a C++ exclusive because they are now available for\u00a0.NET Core (3.0 or higher) in Visual Studio 2019 Preview 2!<\/p>\n","protected":false},"author":651,"featured_media":223603,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[237,1423,9,475],"class_list":["post-223583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-net","tag-breakpoints","tag-debug","tag-visual-studio-2019"],"acf":[],"blog_post_summary":"<p>\u201cWhy is this value changing unexpectedly and where or when is this occurring?!\u201d This is a question many of us dread asking ourselves, knowing that we&#8217;ll have to do some tedious trial-and-error debugging attempting to locate the source of this issue.\u00a0 For C++ developers, the exclusive solution to this problem has been the data breakpoint, a debugging tool that allows you to break when a specific object\u2019s property changes.\u00a0 Fortunately, data breakpoints are no longer a C++ exclusive because they are now available for\u00a0.NET Core (3.0 or higher) in Visual Studio 2019 Preview 2!<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/223583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/651"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=223583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/223583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/223603"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=223583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=223583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=223583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}