Can I set a breakpoint when variable is getting a specific value in .NET?

So long as you are using a Visual Studio edition other than Express, you can achieve this in C# using a breakpoint condition.

In the Breakpoint Condition dialog box, enter a valid expression in the Condition box, such as myLocalVariable > 1

and

...choose Has changed if you want to break when the value of the expression has changed.

To get to the Has changed option, right-click your breakpoint in the Breakpoints window and select Condition..., then check the screenshot below.

Has Changed option for breakpoint conditions


It is certainly possible to set a condition like a variable receiving a certain value. This is known as a breakpoint condition. To create one, do the following.

  • Set a break point at the point the variable changes
  • Right click on the break point and select "Condition"
  • Type in the conditional like "theNewValue == 42"

Now the breakpoint will only hit when your conditional evaluates to true.

The second item you asked for, breaking when a variable's value changes for any reason, is known as a data breakpoint. These are only available for C++ code. It's not an option in C#, VB.NET or any other managed language.