Debugging automatic properties

C#Visual StudioPropertiesBreakpoints

C# Problem Overview


Is there any way to set breakpoint on setter/getter in auto-implemented property?

int Counter { get; set; }

Other than changing it to standard property (I'm doing it in this way, but to do that I have to change and recompile whole project)

C# Solutions


Solution 1 - C#

Using Visual Studio 2008, 2010, 2012, 2013:

  1. Go to the Breakpoint window

  2. New -> Break at Function…

  3. For the get, type: ClassName.get_Counter()

    For the set, type: ClassName.set_Counter(int)

You'll get a "No Source Available" when the breakpoint is hit, but you'll get the calling location in the call stack.

I found this solution here on MSDN

Solution 2 - C#

On Visual Studio 2017:

Hover over "set" word -> right click -> Breakpoint -> Insert Breakpoint

Before:

Before

After:

After

Solution 3 - C#

This question is very old but it is worth noting that it just works in VS 2015.

https://devblogs.microsoft.com/devops/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015/

class X {
  public string name {
    set;
    get; // setting a breakpoint here will break in VS 2015!
  }
}

Solution 4 - C#

If I was you, I'd temporarily make the property a standard one backed by an internal field...set your breakpoints, and then you can change it back after.

Solution 5 - C#

Set Breakpoints where you are setting property or getting property, No other way.

you can do this by Find All References options

And Since it is only storing values and do not have any code in setter part so what do you debug?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarek KwiendaczView Question on Stackoverflow
Solution 1 - C#Matt SmithView Answer on Stackoverflow
Solution 2 - C#andreyk2 HohlovView Answer on Stackoverflow
Solution 3 - C#RedXView Answer on Stackoverflow
Solution 4 - C#KieronView Answer on Stackoverflow
Solution 5 - C#TalentTunerView Answer on Stackoverflow