143

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)

Matt Smith
  • 17,026
  • 7
  • 53
  • 103
Marek Kwiendacz
  • 9,524
  • 14
  • 48
  • 72
  • 2
    What IDE are you working with? Visual Studio? (I suggest you add a corresponding tag to your question, since this is actually more related to a particular IDE than to the C# language itself.) – stakx - no longer contributing Dec 10 '10 at 11:23
  • 5
    @stakx in the .NET world we have a default IDE :) – Stilgar Dec 10 '10 at 11:35
  • 1
    @Stilgar, I'm aware of that, I'm using VS myself, after all. But "default" doesn't mean that it's the only one, nor that *everyone* uses it. Since this is an IDE question, so it's important to know which IDE we're looking at here. Hence my previous comment. – stakx - no longer contributing Dec 10 '10 at 12:16
  • 4
    @stakx well when there is no mention of the IDE for a .NET related question it is assumed that it is VS. What is more I was mostly kidding :) – Stilgar Dec 10 '10 at 12:45

5 Answers5

230

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

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Matt Smith
  • 17,026
  • 7
  • 53
  • 103
  • Briliant. Thanks. I don't need to change automatic properties to standard one (with field) anymore. And no more recompilation:) – Marek Kwiendacz Jul 16 '11 at 22:24
  • 9
    this also works for default constructors. use `ClassName.ctor` – Călin Darie Apr 04 '13 at 13:30
  • 11
    VS2015 has fixed this by just [setting the breakpoints on the auto getter & setter](http://blogs.msdn.com/b/visualstudioalm/archive/2014/11/14/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015.aspx) _:)_ – cateyes May 19 '15 at 22:48
  • 1
    Doesn't work here. I tried the both `Form.set_Height` and `MainWindow.set_Height`, neither worked. – Hi-Angel Aug 03 '15 at 12:46
  • 1
    @Hi-Angel, notice that for setters, you have to include the type, so something like Form.set_Height(int) – Matt Smith Apr 24 '17 at 12:24
31

On Visual Studio 2017:

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

Before:

Before

After:

After

andreyk2 Hohlov
  • 619
  • 2
  • 9
  • 10
9

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!
  }
}
RedX
  • 14,749
  • 1
  • 53
  • 76
7

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.

Kieron
  • 26,748
  • 16
  • 78
  • 122
0

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?

TalentTuner
  • 17,262
  • 5
  • 38
  • 63
  • 3
    yes, but what when you use it in for example 20 places? And every new breakpoint slows down debugging process. Or, what if access to property is done by framework (for example serialization)? – Marek Kwiendacz Dec 10 '10 at 11:04
  • 1
    Conditional break points. Put the break point on, right click it and assign a condition to it... – Phill Dec 10 '10 at 11:45