Visual Studio Tips And Tricks Series - Part One

I used to attend Chapter events, Meetups, and Seminars, apart from blogging & demos. Thus, I got a chance to interact with hundreds of Visual Studio and C# developers. Whenever I meet with any developer, I like to share some Visual Studio tips with them. But, I feel that very few people are having ideas about those tips. So, I decided to write an article on those tips, so that more and more developers can get benefits from this.

I have made a collection of more than 100 Visual Studio tips which can help us maintain standards, save time, have faster development, less-error prone code, and make a smart developer. But, I cannot put all the 100 tips in the same article because it will be lengthy. I will write 6 to 7 articles on it, which will cover the entire collection of Visual Studio tips. Let’s start with some tips without wasting time.

  1. Cycle through last 20 Clipboards

    Cycle

    In Visual Studio, we can cycle through the last 20 copied clipboards. This feature is not a new feature but older than a decade, & available since Visual Studio 2003.

    Let’s have a look at how to do this. I have written a simple entity, Person.
    1. class Person   
    2. {  
    3.     public int Id {  
    4.         get;  
    5.         set;  
    6.     }  
    7.     public int FirstName {  
    8.         get;  
    9.         set;  
    10.     }  
    11.     public int MiddleName {  
    12.         get;  
    13.         set;  
    14.     }  
    15.     public int LastName {  
    16.         get;  
    17.         set;  
    18.     }  
    19.     public int NickName {  
    20.         get;  
    21.         set;  
    22.     }  
    23.     public int Age {  
    24.         get;  
    25.         set;  
    26.     }  
    27.     public string Address {  
    28.         get;  
    29.         set;  
    30.     }  
    31.     public string City {  
    32.         get;  
    33.         set;  
    34.     }  
    35. }  
    Now, just copy 5 properties - Id, FirstName, MiddleName, LastName & NickName, one by one, sequentially.

    Now, press Ctrl+V. You will get last copied data i.e. “NickName” but if you want any other copied data you can use shortcut Ctrl + Shift + V or Ctrl + Shift + Insert.

    NickName – “Ctrl + Shift + V” 1 time
    LastName – “Ctrl + Shift + V” 2 times
    MiddleName – “Ctrl + Shift + V” 3 times
    FirstName – “Ctrl + Shift + V” 4 times
    Id – “Ctrl + Shift + V” 5 times

    It also works with large data. Just add a new class Employee.
    1. class Employee  
    2. {  
    3.     public int Id {  
    4.         get;  
    5.         set;  
    6.     }  
    7.     public string Name {  
    8.         get;  
    9.         set;  
    10.     }  
    11.     public string Designation {  
    12.         get;  
    13.         set;  
    14.     }  
    15.     public double Salary {  
    16.         get;  
    17.         set;  
    18.     }  
    19.     public string Roles {  
    20.         get;  
    21.         set;  
    22.     }  
    23. }  
    Now, copy the complete code of Person class. Then, that of Employee class.

    Employee class complete code -- “Ctrl + Shift + V” 1 time
    Person class complete code -- “Ctrl + Shift + V” 2 times

    You can explore it and use it very easily. This feature is also known as “Clipboard Ring”.

    You can also find this shortcut in edit menu. Edit=> Cycle Clipboard Ring (Ctrl + Shift + V).

  2. Navigate to Backward/Forward

    You can navigate backward and forward throughout the VS code files, using the keyboard shortcut Ctrl + - (hyphen) & Ctrl + Shift + - (hyphen)
    Ctrl + - (hyphen): => Navigate Backward
    Ctrl + Shift + - (hyphen): => Navigate Forward

  3. Quick Launch (Ctrl + Q)

    Quick launch is also one of the best features of Visual Studio. The shortcut is Ctrl + Q. Just press Ctrl + Q and the cursor will be moved to quick launch Textbox. Now, type anything you want to do and it will provide you with all the matching options.

    Quick Launch

    As you can see in the above screenshot, I have typed “new” and it provides me all the options related to it.

    If you are thinking that it is giving you a suggestion, then that's your biggest mistake. It’s not only giving the suggestions but you can say that it is a quick command window where you can run any command. Let’s see how I can launch anything from here.
    In the above screenshot, you can see that currently my cursor is on FileNew Project. Now, just click on it and it will open “New Project” window.

    I think it to be the best feature I have explained so far for Visual Studio.

    Even though I didn’t know all the options available in this window, it’s a vast sea and I think it cannot be explained in a single article.

    But some major features are:

    • Directly run all the matching command (it is showing the complete list of menu where ever matching string found)
    • Options
    • Code Samples
    • Search Nuget Packages

    Let’s search a NuGet package via quick launch. Just click on the option “Search online for Nuget Packages matching …”

    And, it will open.

    open

    Explore the vast sea of Quick launch windows and I am sure that you will find something useful for you.

  4. Align your code

    If you want to align your code, you can align it manually. But, if you like to use some beautiful extensions, you can use these extensions:

    • Align Assignment : Ctrl+ Alt + ]
    • Tabify
    • Code Alignment

    You can use “Align Assignment” or Tabify if you want to align your code assignment, but if you would like more than that, you can use “Code Alignment” extension.

    Code Alignment Shortcuts

    Requirement Shortcut Key
    Align by... (Dialog) Ctrl + Shift + =
    Align by position... (Dialog) Ctrl + =, Ctrl + Backspace
    Align by Equals Ctrl + =, Ctrl + =
    Align by m_ Ctrl + =, Ctrl + m
    Align by " Ctrl + =, Ctrl + '
    Align by . Ctrl + =, Ctrl + .
    Align by Space Ctrl + =, Ctrl + Space

  5. Change Selected text to upper case/lower case
      

    Change the selected text to upper case or lower case using a keyboard shortcut.

    • Change selected text to upper case : Ctrl + Shift + U
    • Change selected text to lower case : Ctrl + U
    • Change selected text to Title case: select the text and right click on it and then choose “Transform Selection – To Title Case”.

  6. Comment/Uncomment Selected code,

    • Comment out the selected lines. Ctrl + E, C
    • Uncomment the selected lines. Ctrl + E, U

  7. Write XML Comments for entire file

    If you have hundreds of public members inside your file and you have to write XML documentation comments for all those members, then you do not have to go to all the members and write XML document comments manually for them separately. Just use the below keyboard shortcut.

    Press Ctrl + A to select entire file and then press Ctrl + Shift + D .

    The keyboard shortcut Ctrl + Shift + D will only work if you have GhostDoc extension installed.

  8. Remove Comments from entire file

    If it is required to remove all the comments from the entire file, you can use the keyboard shortcut:  Ctrl + K, Ctrl + Q

  9. Comment/Uncomment Codes & Remove Comments

    In this trick, the details are about the point “5. Comment/Uncomment Selected code. 6. Write XML Comments for entire file. & 7.Remove Comments from entire file.” has been explained in more depth. If you do not want to go in depth for those points, you can move to the next trick.

    In Visual Studio, we can do commenting and un-commenting of code very easily. Let’s have a look how we can do this. In C#, we can write the comment in 3 ways:

    Single line Comments
    Multi-line Comments
    XML Documentation Comments

    Single line Comments
    1. class Program  
    2. {  
    3. //This is an example of Auto Property Initializer in C# 6  
    4. public string UserName { get; } = "Banketeshvar Narayan Sharma";  
    5. }  
    To write the single line comment, we can just write “//” before the text. We can also use keyboard shortcut to comment it. Select all the lines which you want to comment and press the keyboard shortcut:

    “Ctrl + K, Ctrl + C” or “Ctrl + E, Ctrl + C”

    And, the commented lines would look something like:
    1. //public string ModelId { get; set; }   
    2. //public string Height { get; set; }   
    3. //public string Width { get; set; }   
    4. //public string Thickness { get; set; }  
    5. //public string Weight { get; set; }  
    Note: Single line Comment does mean that it can comment only one line but it indicates that comment chararcters “//” is added to each line independently and any line can be commented or uncommented separately or in a group. To uncomment it just select the commented lines and press keyboard shortcut
    Ctrl + K, Ctrl + U or Ctrl + E, Ctrl + U

    Multi-line Comments

    In case of single line comments we can we use “//” separately for each line and anything written after that in the same line is considered as a comment whereas using multiline comment characters we can comment multiple lines by just commenting some word in between of a line. We use “/*” as comment start signal and “*/” as comment end.
    1. Use /*…*/ for multiple lines  
    2. /* This is an example of Dictionary initializer in C# 6 
    3. Where as in C# 5 we have to write like 
    4. { 101,"Banketeshvar Narayan Sharma"}, 
    5. { 102,"Rajnish Kumar Choudhary"}, 
    6. { 103,"Rajnish Kumar Choudhary"} 
    7. */  
    8. public Dictionary<intstring> _students { get; } = new Dictionary<intstring>()  
    9. {  
    10. [101] = "Banketeshvar Narayan Sharma",  
    11. [102] = "Rajnish Kumar Choudhary",  
    12. [103] = "Bhupesh"  
    13. };  
    14.   
    15. Use /*…*/ for single line  
    16. public string ModelId { getset; } /* = string.Empty; */  
    17. Use /*…*/ for commenting in middle of a line  
    18. public string ModelId /*{ get; set; }*/ = string.Empty;  
    XML Documentation Comments
    1. /// <summary>  
    2. ///   
    3. /// </summary>  
    4. /// <param name="emailId"></param>  
    5. /// <param name="subject"></param>  
    6. /// <param name="msgBody"></param>  
    7. /// <param name="listOfCcEmailIds"></param>  
    8. /// <param name="listofBccEmailIds"></param>  
    9. public void SendMail(string emailId, string subject, string msgBody, string[] listOfCcEmailIds, string[] listofBccEmailIds)   
    10. {  
    11.     //send mail.  
    12. }  
    To write the above xml comment just type “///” and press enter it to automatically generate these codes:
    1. /// <summary>  
    2. ///   
    3. /// </summary>  
    4. /// <param name="emailId"></param>  
    5. /// <param name="subject"></param>  
    6. /// <param name="msgBody"></param>  
    7. /// <param name="listOfCcEmailIds"></param>  
    8. /// <param name="listofBccEmailIds"></param>  
    But, if you look into the xml comment done by ‘writing “///” and pressing enter’, you will notice that the summary part is empty and you have to write the summary manually. But, writing the summary manually is not a good idea because your other team members may write in different ways or sentences, which may lead to breaking the standard of your code.

    So, we can take advantage of some extensions, like GhostDoc. You can also use other extensions but I am going to use GhostDoc for xml comments.

    We can write an xml Comment for a single property, method, class, interface, or any other type; or we can do xml comment for entire file.

    Comment a Single Method/property
    1. public int Id { getset; }  
    2.   
    3. /// <summary>  
    4. /// Gets or sets the first name.  
    5. /// </summary>  
    6. /// <value>  
    7. /// The first name.  
    8. /// </value>  
    9. public int FirstName { getset; }  
    10.   
    11. public int MiddleName { getset; }  
    12.   
    13. public int LastName { getset; }  
    In the above example, I have just selected the property FirstName and pressed the shortcut:

    “Ctrl + Shift + D”

    Comment Entire File

    To Comment the entire file, just select the entire file by keyboard shortcut “Ctrl + A” and then press “Ctrl + Shift + D”. It will write XML comment for the entire file. I am not writing the entire file here but you can download the file with name BeforeXMLComment.cs & AfterComment.cs

    Removing Comments

    I talked a lot about commenting and uncommenting a file, but sometimes, we may need to remove comments. Let’s see how we can remove the comments.

    Select the entire content of the file AfterComment.cs by pressing “Ctrl + A” then press Ctrl + K, Ctrl + Q. it will remove all the comments and it will look like the previous file BeforeXMLComment.cs. I am using Comment Remover extension for this.

    We can exclude XML comment also using GhostDoc but it adds “exclude” which looks as an extra line.
    1. /// <exclude />  
    2. public int FirstName { getset; }  
    But using comment remover extension we can remove it very easily.

    So far we have discussed the following keyboard shortcuts for comment/uncomment/Remove Comment:

    Ctrl + K, Ctrl + C => Comment out the selected lines.
    Ctrl + E, Ctrl + C => Comment out the selected lines.
    Ctrl + K, Ctrl + U => Uncomment the selected lines.
    Ctrl + E, Ctrl + U => Uncomment the selected lines.
    /// => Automatic XML comment for a property/method/types…
    Ctrl + shift + D => Write descriptive & meaningful XML comment for a selected property/method/types…
    Ctrl + K, Ctrl + Q => Remove all Comments
    You can find following option for Comments inside Visual studio
    Edit => Comments

    Comments

    And inside GhostDoc

    GhostDoc

    You can find GhostDoc inside Tools Menu.

  10. Sort selected lines

    You can sort the selected lines in ascending or descending order very easily.
    Sort selected lines in Ascending Order: Alt + 3.

    To sort lines in ascending order you just need to select required lines and press Alt + 3.
    Sort selected lines in Descending Order: Alt + 4.

    To sort lines in descending order you just need to select required lines and press Alt + 4.

  11. Reorder your Code

    Reordering of code is very easy in Visual Studio and it can be done by a single keyboard shortcut:

    Move code Up: Alt +Up Arrow
    Move Code Down: Alt + Down Arrow

    Reorder

    In the above screenshot, you can see that I have moved a line below and to do that I have just selected the line and pressed the keyboard shortcut Alt +Down Arrow.

  12. Remove Empty and duplicate lines

    We can remove empty and duplicate lines very easily.

    Remove Empty lines
    Select all the lines from which you want to remove empty lines and right click on it – “Selected Lines” – “Remove Empty Lines”.

    Remove Duplicate Lines
    Select all the lines from which you want to remove duplicate lines and right click on it – “Selected Lines” – “Remove Duplicate Lines”.

    Remove Duplicate Lines

    Note: It requires web essentials extension to be installed in your visual studio for “Sort selected lines” & “Remove Empty and duplicate lines”.

  13. Use outlining shortcuts

    Use Ctrl + M + O for collapsing all the code to definition rather than collapsing all the expanded sections one by one. You can also collapse the expanded code section by right clicking on editor window and selecting options from context menu. The complete list of outlining shortcuts is below:

    Requirement Shortcut Key
    Stop Outlining Ctrl+M, Ctrl+P
    Ctrl+M, P
    Toggle All Outlining Ctrl+M, Ctrl+L
    Ctrl+M, L
    Toggle Outlining Expansion Ctrl+M, Ctrl+M
    Ctrl+M, M
    Collapse All Outlining Ctrl+M, Ctrl+A
    Expand All Outlining Ctrl+M, Ctrl+X

  14. Code Snippet

    Code snippet is one of the best features of visual studio and it provides hundreds of in-built code snippets.
    To use a code snippet we need to write that snippet and pres the tab key twice to complete the code. Below are some examples of code Snippets:

    C# Code Snippet

    Code Snippet Details
    ctor Write constructor if class name is User it will generate code
    public User()
    {

    }
    for It will write entire for loop.
    for (int i = 0; i < length; i++)
    {

    }
    forr Reverse for loop.
    for (int i = length - 1; i >= 0; i--)
    {

    }

    JavaScript Code Snippet

    Code Snippet Details
    cl console.log();
    forin for (var i in o) {

    }
    function function myfunction() {

    }

    HTML Code Snippet

    Code Snippet Details
    a <a href=""></a>
    img <img src="" alt="" />
    span <span></span>

    Apart from that, there are thousands of code snippets for C#, VB.NET, HTML and JavaScript that can be downloaded from visual studio extensions.

  15. Write your Code Snippet
    You can write your own code snippet too and it is very easy. You can do it in a few minutes. Let’s have a look how you can do this.

    a. Write your code snippet File and save it with extension .snippet. For reference you can go through hundreds of in-built code snippets already available in your local drive.

    Code Snippet

    For Visual studio 2015, you can find it at “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#”
    If you didn’t find code snippets for reference at the specified path, then open Visual studio 2015 and press the keyboard shortcut: Ctrl + K, Ctrl + B you can also find it from Tools menu—“Code Snippet Manager…”

    Code Snippet Manager

  16. Insert code Snippet ( Ctrl +K, X )
    If you do not know which code snippet you should use or you would like to see the list of available snippets, you can use the keyboard shortcut “Ctrl +K, X”.

Up Next
    Ebook Download
    View all
    Learn
    View all