Debugging automatic properties
Asked Answered
S

5

148

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)

Switcheroo answered 10/12, 2010 at 10:57 Comment(4)
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.)Misname
@stakx in the .NET world we have a default IDE :)Anecdotal
@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.Misname
@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 :)Anecdotal
F
233

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

Fondea answered 15/7, 2011 at 22:34 Comment(5)
Briliant. Thanks. I don't need to change automatic properties to standard one (with field) anymore. And no more recompilation:)Switcheroo
this also works for default constructors. use ClassName.ctorTrioecious
VS2015 has fixed this by just setting the breakpoints on the auto getter & setter :)Suttles
Doesn't work here. I tried the both Form.set_Height and MainWindow.set_Height, neither worked.Rausch
@Hi-Angel, notice that for setters, you have to include the type, so something like Form.set_Height(int)Fondea
K
38

On Visual Studio 2017:

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

Before:

Before

After:

After

Kenward answered 1/4, 2020 at 16:53 Comment(2)
Work with VS2019Iberian
Thank you, this was the correct answer and still works with VS2022Thinskinned
C
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!
  }
}
Coracoid answered 8/3, 2017 at 14:35 Comment(0)
E
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.

Epigene answered 10/12, 2010 at 11:33 Comment(0)
L
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?

Lucilelucilia answered 10/12, 2010 at 11:1 Comment(2)
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)?Switcheroo
Conditional break points. Put the break point on, right click it and assign a condition to it...Partridge

© 2022 - 2024 — McMap. All rights reserved.