Tricks And Shortcuts To Use Visual Studio Like A Pro and Double Your Code Speed

In this article I’m going to show you lots of tricks and shortcut keys in Visual Studio so you can write code faster.

To write code faster you need to be relying more on the keyboard than the mouse. Switching between your mouse and keyboard actually slows you down. Here is a handy reference that can make your .NET lifestyle a bit easier and more productive.

So let’s get started.
 
Code Snippet Methods Shortcuts
 
Write ‘for’ and press tab to get a for loop. You will get it like this,
  1. for (int i = 0; i < length; i++)  
  2. {  
  3. }  

For recursive for loop, write ‘forr’ and press tab.

  1. for (int i = length - 1; i >= 0; i--)  
  2. {  
  3. }   
 For foreach loop write 'foreach', then press tab. It will generate code like this,
  1. foreach (var item in collection)  
  2. {  
  3. }  
 To generate static void main method, just write ‘svm’ and press tab.
  1. static void Main(string[] args)  
  2. {  
  3. }  

To generate a try catch block inside your method just write ‘try’ and press tab.

  1. try  
  2. {  
  3. }  
  4. catch (Exception)  
  5. {  
  6. throw;  
  7. }  

To generate a try finally block, write ‘tryf’ and press tab.

  1. try  
  2. {  
  3. }  
  4. finally  
  5. {  
  6. }  

To generate a property write ‘prop’ and then press tab. You will get like this:

  1. public int MyProperty { getset; }  

To generate full property stub, write ‘propfull’

  1. private int myVar;  
  2. public int MyProperty  
  3. {  
  4. get { return myVar;}  
  5. set { myVar = value;}  
  6. }  

To generate read-only property , write ‘propg’ and then press tab

  1. public int MyProperty { getprivate set; }   

To generate a class, write 'class', then press tab

  1. class MyClass  
  2. {  
  3. }  
 Similarly here is the full shortcut list, Have a look:
  • '#if Creates a #if directive and a #endif directive.
  • '#region Creates a #region directive and a #endregion directive.
  • ~ Creates a destructor for the containing class.
  • attribute Creates a declaration for a class that derives from Attribute.
  • checked Creates a checked block.
  • class Creates a class declaration.
  • ctor Creates a constructor for the containing class.
  • cw Creates a call to WriteLine.
  • do Creates a do while loop.
  • else Creates an else block.
  • enum Creates an enum declaration.
  • equals Creates a method declaration that overrides the Equals method defined in the Object class.
  • exception Creates a declaration for a class that derives from an exception (Exception by default).
  • for Creates a for loop.
  • foreach Creates a foreach loop.
  • forr Creates a for loop that decrements the loop variable after each iteration.
  • if Creates an if block.
  • indexer Creates an indexer declaration.
  • interface Creates an interface declaration.
  • invoke Creates a block that safely invokes an event.
  • iterator Creates an iterator.
  • iterindex Creates a "named" iterator and indexer pair by using a nested class.
  • lock Creates a lock block.
  • mbox Creates a call to MessageBox.Show. You may have to add a reference to System.Windows.Forms.dll.
  • namespace Creates a namespace declaration.
  • prop Creates an auto-implemented property declaration.
  • propfull Creates a property declaration with get and set accessors.
  • propg Creates a read-only auto-implemented property with a private "set" accessor.
  • sim Creates a static int Main method declaration.
  • struct Creates a struct declaration.
  • svm Creates a static void Main method declaration.
  • switch Creates a switch block.
  • try Creates a try-catch block.
  • tryf Creates a try-finally block.
  • unchecked Creates an unchecked block.
  • unsafe Creates an unsafe block.
  • using Creates a using directive.
  • while Creates a while loop.

If these shortcuts are difficult to remember you then no worry, here is trick for that too. Just press

Ctrl+K+X to insert snippet in your code.

It will look like this,

ASP.NET

ASP.NET
 

Now comes the Text Navigation in Visual Studio. 
  1. Use Ctrl + arrow -> or <- to move one word at a time.
  2. Use Home to get on the start of line
  3. Page Down to scroll down
  4. Page up to scroll up
  5. Ctrl+Home to get on the beginning of page.
  6. Ctrl+end to get on the end of the page.

Text Manipulation

  1. Ctrl+J: List Members to complete statement while editing code.
  2. Ctrl-U Changes the selected text to lowercase characters
  3. Ctrl-Shift-U Changes the selected text to uppercase characters

Commenting Code

Select block of code and then press Ctrl+K to comment and Ctrl+U to uncomment.

Collapsing block of code

Ctrl+M,Ctrl+M to collapse a code and same to expand the code.

Text Selection(A handy way)

Suppose you have declared many numbers of variables and specified modifier as ‘public’ and you need to change it to private then you can simply use the below tricks to perform the above operation smartly.

Select by Ctrl+Alt+Down Arrow Key to the point you need to select. Then write whatever modiefier you want to specify. You can delete backward also. I am attaching gif image for your understanding.


Selecting a tab

  1. Press Ctrl+Tab to cycle through opened tabs in forward direction.
  2. Ctrl+Shift+Tab to cycle through tabs in reverse direction.

    ASP.NET

Formatting the code

If the code is scattered on your page, then format it press Ctrl+K+D. All the code will be formatted properly by doing so.

Debugging and Running

  1. Ctrl+F5 to run application
  2. F5 to run the application in the debug mode
  3. Shift + F5 to stop the debugging session
  4. F9 to insert or remove a breakpoint
  5. F10 to step over a method
  6. F11 to step into a method
  7. Shift + F11 to step out of a method

Design and Code Behind Switching

  1. If you are on Code behind and wanted to go to source then press Shift+F7 Twice.
  2. If you want to go to design then press Shift+F7 once.
  3. Similarly, if you are on the source page or design page and wanted to jump to code behind of that page then just press F7

Summary

By practicing these shortcuts and tricks you can develop code faster, like a beast. These will benefit you in many ways. We can more easily access a variety of commands and windows in Visual Studio by choosing the appropriate keyboard shortcut.


Similar Articles