Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick-delete surrounding statements in Visual Studio or Resharper

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?
like image 330
Cel Avatar asked Oct 02 '14 12:10

Cel


3 Answers

Shift+delete to cut IF line

Alt+Enter on bracket to remove redundant braces.

like image 124
Tomasito Avatar answered Oct 15 '22 05:10

Tomasito


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.

like image 2
Roger Lipscombe Avatar answered Oct 15 '22 06:10

Roger Lipscombe


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

like image 1
Kjartan Avatar answered Oct 15 '22 04:10

Kjartan