14

I often find I need to remove nesting statements, say an if conditional becomes irrelevant:

From

if (processFile != null && processFile.Exists)
{
    Process[] processesByName = GetProcesses(processFile.NameWithoutExt);
    if (processesByName.Length > 0)
    {
        return processesByName.ToList();
    }
}

return null;

To

Process[] processesByName = GetProcesses(processFile.NameWithoutExt);
if (processesByName.Length > 0)
{
    return processesByName.ToList();
}

return null;

The trouble is having to manually find the curly braces on both sides and delete them, while retaining the nested code

  • Especially with larger bodies, unlike the example here
  • Any way to quick-erase with Resharper?
  • Or in Visual Studio natively?
1
  • I usually delete the if line manually, then position the cursor at the first curly and invoke "remove unnecessary curly braces" (alt+ enter, I believe) Commented Oct 2, 2014 at 13:00

3 Answers 3

12

Shift+delete to cut IF line

Alt+Enter on bracket to remove redundant braces.

5
  • this sounds like the best solution thus far, but was thinking of a general solution for which it does not work e.g. to remove try .. catch or to remove if .. else .. (and i guess in the latter case the code inside else should be deleted as well)
    – Cel
    Commented Oct 2, 2014 at 13:08
  • 1
    Use ctrl+shift+] on starting bracket to select code inside it
    – Tomasito
    Commented Oct 2, 2014 at 13:11
  • Alt+Enter opens Property window for me (Visual C# 2005 keyboard mapping, VS2013 Express). I am not sure if it's default, could you tell which command is executed by pressing Alt+Enter for you?
    – Sinatr
    Commented Oct 2, 2014 at 13:41
  • I have VS2010+Resharper+CodeMaid, when I'm on bracket I see hammer icon on left side of screen, hover cursor on it and check tool tip for shortcut.
    – Tomasito
    Commented Oct 2, 2014 at 14:30
  • 1
    Alt+Enter does not work in VS. Any idea how to make it work without ReSharper in VS 2017?
    – Tecman
    Commented Jul 3, 2017 at 7:49
2

Change the condition to if (true || whatever)? I think that ReSharper will then tell you that the condition is always true, and will offer to remove it.

1

One solution, although it might not be ideal:

  • Click and drag to mark the code you want to keep.
  • Use the Resharper command "Surround with..." and select "#region".
  • Now you can collapse the code you want to keep using the minus sign at the top of the new region.
  • Remove the code surrounding the #region
  • Now click the #region title again and select the Resharper option "Remove region/endregion directives".

Not a perfect solution, but it should help you get a better overview of what you're doing when working with larger blocks of code than your OP example.

It should look something like this (where the #region directive can hide any of lines of code):

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.