{"id":56470,"date":"2014-11-14T16:37:44","date_gmt":"2014-11-15T00:37:44","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/devops\/?p=56470"},"modified":"2019-05-03T16:41:43","modified_gmt":"2019-05-04T00:41:43","slug":"set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015\/","title":{"rendered":"Set breakpoints on auto-implemented properties with Visual Studio 2015"},"content":{"rendered":"<p>Ask a room of developers what is the first concept that they think of when they think about debugging and most of them will say: breakpoints. That is why <a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2014\/10\/06\/new-breakpoint-configuration-experience.aspx\">with Visual Studio 2015 we significantly improved the ability to configure breakpoints with conditions and actions<\/a>. Given how easy it is now to configure breakpoints, we know you&rsquo;ll be more upset if you accidentally delete one of them, so we also offer undo capability &ndash; when you remove a breakpoint by accident, just Ctrl+Z to bring it back with all its settings.<\/p>\n<p>In this post, I&rsquo;ll share another smaller breakpoint enhancement: the ability to place breakpoints on auto-implemented properties.<\/p>\n<h2>The problem (up to VS2013)<\/h2>\n<p>C# supports <a href=\"http:\/\/msdn.microsoft.com\/library\/bb384054.aspx\">auto-implemented properties<\/a>. While debugging, it is sometimes useful to break when a property is accessed, especially when it is called from multiple places and it is tedious to track them all down to set a breakpoint on all the caller sites. So considering the following example&hellip;<\/p>\n<pre>namespace MyName {\n  class Program {\n    static bool MyProperty {\n      get; \/\/ line 4\n      set; \/\/ line 5\n    }\n    static void Main() {\n      if ((new System.Random()).Next(3) == 1)\n        MyProperty = true; \/\/ line 9\n      else\n        MyProperty = false; \/\/ line 11\n      System.Diagnostics.Debugger.Break(); \/\/ line 12\n    }\n  }\n}<\/pre>\n<p>&hellip;if you try to set a breakpoint on lines 4 or 5, the breakpoint will unfortunately move to the closest place where we think it will <a href=\"http:\/\/msdn.microsoft.com\/library\/bb146593.aspx\">bind<\/a>. <em>Aside: where we think it will bind differs depending on whether you set it at design time or at runtime which is when we have more info.<\/em> Either way it will not break, and when you F5 it you&rsquo;ll break instead directly on line 12.<\/p>\n<p>First I&rsquo;ll share the VS2013 workaround, and then the VS2015 solution &ndash; feel free to skip to the next section with the VS2015 solution if that&rsquo;s all you care about.<\/p>\n<h2>The Visual Studio 2013 solution &ndash; function breakpoints to the rescue<\/h2>\n<p>Other than tracking down all the caller sites, or changing the auto-implemented property to be a full property with an explicitly visible backing field in your code, the solution is to use a <a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2013\/10\/08\/function-breakpoints.aspx\">Function Breakpoint<\/a>. You don&rsquo;t know about function breakpoints you say? Go <a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2013\/10\/08\/function-breakpoints.aspx\">read about them<\/a> and I&rsquo;ll wait here for you.<\/p>\n<p>So from the Breakpoints window set a new Function breakpoint (Ctrl+B) as per the following screenshot (in this case simply entering <em>set_MyProperty<\/em> would also work)<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image002\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/7506.clip_image002_7B24B6B6.jpg\" alt=\"clip_image002\" width=\"546\" height=\"286\" border=\"0\" \/><\/p>\n<p>Now when you F5 the debugger will break on line 9 or 11, dependent on which one was the lucky one making the call.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image003\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/4375.clip_image003_53EA6D81.png\" alt=\"clip_image003\" width=\"513\" height=\"417\" border=\"0\" \/><\/p>\n<p><em>Note: if you have turned off Just My Code (JMC) from Debug-&gt;Options, or you have checked the &ldquo;Show External Code&rdquo; option in the Call Stack window, then you&rsquo;ll need to navigate in the Call Stack to the top most user code function which will be Main, and then you&rsquo;ll see it on line 9 or 11.<\/em><\/p>\n<h2>The Visual Studio 2015 solution &ndash; it just works<\/h2>\n<p>In Visual Studio 2015, simply set the breakpoint on line 4 or 5 and it works. It will break there, and JMC\/External Code considerations are not relevant. To navigate to the caller, simply go down the stack by one stack frame and you&rsquo;ll land on line 9 (or 11).<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image004\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/6332.clip_image004_56932932.png\" alt=\"clip_image004\" width=\"710\" height=\"467\" border=\"0\" \/><\/p>\n<p>You can thank the Roslyn team for fixing this, and we are excited that we are closing <a href=\"http:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio\/suggestions\/2113391-setting-breakpoints-on-auto-implemented-properties\">yet another uservoice item<\/a> with this release. As always your feedback is welcome via <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/vsdebug\/threads\">our MSDN forum<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ask a room of developers what is the first concept that they think of when they think about debugging and most of them will say: breakpoints. That is why with Visual Studio 2015 we significantly improved the ability to configure breakpoints with conditions and actions. Given how easy it is now to configure breakpoints, we [&hellip;]<\/p>\n","protected":false},"author":241,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[224],"tags":[],"class_list":["post-56470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure"],"acf":[],"blog_post_summary":"<p>Ask a room of developers what is the first concept that they think of when they think about debugging and most of them will say: breakpoints. That is why with Visual Studio 2015 we significantly improved the ability to configure breakpoints with conditions and actions. Given how easy it is now to configure breakpoints, we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/56470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/241"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=56470"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/56470\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=56470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=56470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=56470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}