Captain Comic Captain Comic - 4 months ago 11
C# Question

Conditional breakpoint in Visual Studio

I want to set a breakpoint on a certain line in C# code when some other variable is equal to a specific value, say:

MyStringVariable == "LKOH"


How can I do that?

I tried to right click on breakpoint icon -> Condition and then typed
MyStringVariable == "LKOH"
and Visual Studio said it cannot evaluate it.

Answer

Sample code:

static void Main(string[] args) {
  string myvar;
  for (int ix = 0; ix < 10; ++ix) {
    if (ix == 5) myvar = "bar"; else myvar = "foo";
  }    // <=== Set breakpoint here
}

Condition: myvar == "bar"

Works well.

Comments