Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015 How to quickly move existing code after if statement into newly placed curly braces?

So if I have something like this:

 if (a>b)
      return true;

and I decide later that I want to change it to this:

if (a>b)
{
     a++;
     return true;
}

Is there a quicker way to have the existing code under the if statement go directly into the newly made curly braces? Because currently if I were to add the curly braces after creating the if statement in the first example I'd have to cut the "return true" from under the new braces and paste it between the newly made curly braces. This is what it looks like:

if (a>b)
{
}
return true;

and it's pretty annoying. Is there a fix to this or do we have to manually copy and paste the existing line in between the brackets?

P.S. I'm using Microsoft Visual Studio 2015 ver 3 and programming in c# but I think this problem occurs in other languages too like c++.

like image 868
Capn Jack Avatar asked Sep 23 '16 20:09

Capn Jack


People also ask

How do you match braces in Visual Studio?

Go to matching brace in Visual Studio with Ctrl+} shortcut To move the caret to the matching brace of the one it's currently on press Ctrl+}. This is very handy for navigating code which might have multiple levels of if/else block nesting, although do try and avoid this kind of code by using guard clauses if you can.

How to Go forward in Visual Studio?

CompleteWord in Microsoft Visual Studio, Alt+Right navigates forward.

How to navigate through code in Visual Studio?

You can use the navigation bar (the drop-down boxes at the top of the code window) to navigate to code in a codebase. You can choose a type or member to go directly to it. The navigation bar appears when you edit code in a Visual Basic, C#, or C++ code base.


1 Answers

Highlight the code and then press Alt + Up Arrow. Move code down with Alt + Down Arrow. Works with multiple lines too. Plus, you don't have to highlight the entire line. As long as one character is highlighted, it works.

https://blogs.msdn.microsoft.com/zainnab/2013/07/01/visual-studio-2013-preview-moving-lines-of-code/

like image 99
Alex Avatar answered Oct 12 '22 23:10

Alex