简体   繁体   中英

VS2005: when stepping through C# code, is there way to skip through sections of code?

What can be done to skip through the parts of code when stepping through code? I find this particularly annoying when the debugger jumps to property gets and sets. Is there way to avoid this?

If you want to skip an entire method you can mark it with the DebuggerStepThrough attribute:

[DebuggerStepThrough]
public void SomeMethod()
{
    // lots of code...
}

public int SomeProperty
{
    [DebuggerStepThrough] 
    get { return ComplexLogicConvertedToMethod(); } 
    [DebuggerStepThrough]      
    set { this.quantity = value ; }
}

Note that the attribute prevents the debugger from stepping into the method or property, but you can always place a breakpoint in that method and stop there 1 .

The attribute comes in handy especially when you have code like this:

DoSomething(obj.SomeProperty);

If you want to step into DoSomething and press F11 you will - without the attribute - first step into SomeProperty and then into DoSomething . With the attribute however, you end up immediately in the DoSomething method.

1 If you want to completely prevent users from placing a breakpoint into a method you can use the DebuggerHiddenAttribute .

there's an option Step over properties and operators (Managed only)

or use F10 instead of F11 (with default keyboard binding)

是的,有一个跨步(F10)功能和一个跨步至(F11)。

您可以使用“运行至光标”设置一个断点。

When you used F10 the code simply steps over each statement unless you've set a break point in a deeper level. I've never found the debugger miss behave the way you've suggested , mind you I'm only using VS2008.

You can set the attribute DebuggerStepThroughAttribute on any methods/properties you don't want to step into.

And you can also use the "Step Over" rather than "Step Into" command.

Add DebuggerStepThrough attribute to your property:

[DebuggerStepThrough]
private static void DO() {
  Console.WriteLine("test");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM