Skip debugging delegate wrapper - why inconsistent behaviour?

ddomen 1 Reputation point
2022-01-24T13:08:34.637+00:00

I'm used to employ DebuggerNonUserCode/DebuggerStepThrough attributes to improve the debugging of my code, but when I wrote a wrapper for a delegate I struggled to step inside it.

After some investigation and time I found out that by using any property (or either an implicit conversion operator) inside the method marked with debug-skipping attributes makes the debugger completely ignore any other method/function invoked inside it.

Here the simpliest ad-hoc built example that came to my mind to explain what I'm talking about:

using System.Diagnostics;
public class Test {

    public int Field;
    public int Property { get; set; }

    [DebuggerStepThrough]
    public int Unsteppable(Func<int, int> wrapped) => wrapped(Property);

    [DebuggerStepThrough]
    public int Steppable(Func<int, int> wrapped) => wrapped(Field);
    public static void Main(string[] args) {
        Test test = new Test();
        int unstep = test.Unsteppable(property => {
            // Here is impossible to step inside the lambda function
            // without using breackpoints
            return property + 5;
        });
        int step = test.Steppable(field => {
            // Here I can step easily by using F11 in VS
            return field + 5;
        });
    }
}

I was wondering if there are any reasons behind the inconsistency between these two behaviours.

(Tested in both VS 2019 and 2022)

So the question is:

should this being intended in this way or is it a flaw in the debugger?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,413 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
954 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 28,056 Reputation points Microsoft Vendor
    2022-01-25T07:47:30.497+00:00

    Hi @ddomen ,

    Welcome to Microsoft Q&A forum.

    I think the inconsistent behavior may be caused by Step over properties and operators (Managed only) option. Will it work if you try to disable Step over properties and operators (Managed only) option(Tools > Options > Debugging > General)?

    Step into properties and operators in managed code

    Best Regards,
    Tianyu

    • If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    1 person found this answer helpful.