Multi-statement line breakpoints

How to create breakpoints on part of a multi-statement line

Home DailyDrop

Daily Knowledge Drop

When a line of code contains multiple statements, the Toggle Breakpoint shortcut F9, in combination with the I-beam position, can be used to create a breakpoint on a single statement in the line.

This post refers to Visual Studio functionality - the experience might differ with other IDEs.


The problem

When a single line contains multiple statements, creating a breakpoint by clicking in the row margin creates a breakpoint on the entire line.

Consider the code example:

var rando = new Random();
var randomValue = rando.Next(0, 100);

Console.WriteLine(ExecuteFunction(() => $"The random value is: {randomValue}"));

string ExecuteFunction(Func<string> func)
{
    return func.Invoke();
}

The intention is to create a breakpoint on the lambda function on line 4, to confirm the value of randomValue when the string is generated - however creating a breakpoint using the row margin will result in a breakpoint on the entire line.

Line breakpoint

This doesn't help in this case, as the breakpoint will be hit when Console.WriteLine is executed, not when the lambda is being executed.

The resolution

Code format

One option to be able to set the breakpoint on the single statement, is to re-format the code so each statement is on its own line:

var rando = new Random();
var randomValue = rando.Next(0, 100);

Console.WriteLine(
    ExecuteFunction(
        () => 
            {
                return $"The random value is: {randomValue}";
            }
        )
    );

string ExecuteFunction(Func<string> func)
{
    return func.Invoke();
}

A breakpoint can now be set by clicking the margin on line 8, and will be hit when the lambda is executed:

Reformatted code breakpoint

Shortcut

Another easier way is to use the F9 Toggle Breakpoint shortcut, in combination with the I-beam position to create a breakpoint on a specific statement, not on a specific line.

Clicking on Console.WriteLine (so that the I-beam is blinking anywhere in the words) , and hitting F9 will result in the Console.WriteLine statement (in this case, the entire line) getting the breakpoint:

Console.WriteLine statement breakpoint

However, clicking on the $"The random value is: " string (which is the return statement for the lambda expression) and hitting F9, will result in that statement getting the breakpoint:

Lambda statement breakpoint


Notes

The ability to set a breakpoint on a specific statement is an incredibly useful feature, I wish I had known about sooner - but now that I am aware of it, it will be made use of daily.


Daily Drop 68: 06-05-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net visualstudio breakpoint debugging