Visual Studio Tips And Tricks Series - Part Three

This article is the third part of the series of articles written on Visual Studio Tips & Tricks.

In the previous two articles, I have explained 31 tips. Now, I am continuing from number 32.
  1. Enjoy the CodeLens feature of Visual Studio

    CodeLens is one of the best features provided by the Visual Studio code editor. It has been available with Visual Studio for a very long time but earlier, this feature was only available withthe ultimate edition. Whereas in Visual Studio 2015, it is available with the professional edition too. Given below is a screenshot of CodeLens.

    code

    As you can see, it is giving a lot of information which is required by developers during development.

    It is showing the list of references along with the file name & line number and highlighting the reference. So now, you do not need to open Find all references window.

    Along with that, it is also showing the username (author) who has changed the code. As you can see in the above screenshot, it is showing only 1 author name because only 1 author has edited it 5 minutes ago.

  2. Take benefits of Bing Contextual Search

    Bing Contextual Search is a very convenient way to search the online references while writing your code, without leaving your lovely editor. Below is a screenshot of Bing Contextual Search.

    code

    To know more about Bing Contextual Search, go through the following link:

    Use Developer Assistant Extension with Visual Studio 2015

  3. Rename

    To rename a method, property, variable, or type use the keyboard shortcut: F2

    Rename

    In the above screenshot, you can see that I have just clicked on the parameter variable “userId” and pressed F2. It starts highlighting all the references; when renamed it will update all the references. It also provides a window using which you can choose other options:

    Include Comments, Include strings, Include Changes.

  4. Surround with…

    Surround with…
    We can use “Surround with…” feature of Visual Studio to surround our code. To use it, just select the code which you would like to surround and press the keyboard shortcut: Ctrl + K + S. Now, choose from the list of the options available for surrounding the code.
    For C# language, the following surrounds with options are available:

    #if, #region, Checked, class, do, else, enum, for, foreach, forr, if, interface, lock, namespace, try, tryf, unchecked, unsafe, using, while.

    Depending on language, Visual Studio edition, and Visual Studio versions, the above list may differ.

  5. Execute in Interactive

    Execute in Interactive

    Execute Interactive is a new feature in Visual Studio 2015 (SP2 and onwards) which allows us to execute our code snippet (a section of your code) without compiling the complete application. If you are interested to know more about “Execute Interactive”, you can go through these articles:

    Using C# Interactive With Visual Studio 2015
    Using C# Interactive With Visual Studio 2015 - Part Two

  6. Quick Actions and Refactorings…

    The shortcut for “Quick Actions and Refactorings…” is Ctrl + Alt + . Or Ctrl + .

    Example 1:

    shortcut

    As you can see in the above image, it is prompting for removing one parameter which is not being used.

    Example 2:

    shortcut

    Example 3:

    shortcut

    But in Visual Studio 2015, it has been enhanced a lot and you can explore more about “Quick Actions and Refactorings…” in the below article:

    Auto Suggest With Spell Check and Quick Fix: Visual Studio 2015 Update 2

  7. Organize Usings

    In Visual Studio 2013, we use Organize Using feature for “Remove Unnecessary Usings, Sort Usings, Remove and Sort Usings”, as shown in the below screenshot.

     Organize Usings

    But in Visual Studio 2015, the light bulb option provides the feature to remove it very easily and it is more user friendly than that in VS 2013. 

     Organize Usings


    So, we can say that in Visual Studio 2015, Organize Using has been changed a lot and now, it’s more user friendly. As you can see in the below screenshot, the namespaces which are not being used, are greyed out.

    namespaces

    But, this is not limited to namespaces but also with types. I added a type in using block and that is also greyed out because it is not being used anywhere in the project.

    namespaces

    If you are not aware about using types in using block instead of namespaces, then you can go through the below links:

    Using Struct as Directive in C# 6 & C# 7
    Using Static Classes as Directive in C# 6
    Using Non-Static classes in using block as directive in C# 6
    Using Static Preferences and Conflicts: C# 6 And C# 7
    Using Enum in using Block as Directive in C# 6

  8. Diagnostic Tools

    In Visual Studio 2015, there is a new feature of Diagnostic Tools. The Diagnostic Tools is a window which provides us the feature to diagnose our application very easily.

    Diagnostic Tools

    Before Visual Studio 2015, it was a very tough task to diagnose the running program and we needed to use different types of diagnostic tools . Some of them were having awkward UI and some were paid versions or third party tools. But now, we do not need to worry about those things because now, we have diagnostic tools window in Visual Studio 2015.

  9. Encode/Decode Selection

    In Visual Studio piece of code can be encoded very easily.

    Encode/Decode Selection

    So, HTML Encode for “<b>Banketeshvar Narayan<b>” will be “&lt;b&gt;Banketeshvar Narayan&lt;b&gt;”

    In the same way, you can explore it and encode/decode your selection for HTML Encode, HTML Attribute Encode, HTML Decode, URL Encode, URL Decode & JavaScript String Encode.

  10. Reverse a string

    A string can be reversed in Visual Studio in a single click. So, a string “Banketeshvar Narayan” can be reversed to “nayaraN ravhseteknaB”.

    Select the string – Right Click – Transform Selection -- Reverse,

    Reverse a string

  11. Remove Diacritics

    Diacritics can be removed very easily from a string, as you can see in the below screenshot:

    Remove Diacritics

    Select the string – Right click – Transform selection – Remove Diacritics. So, text “diakritikós” will be converted to “diakritikos”.

  12. Encrypt a string

    In Visual Studio, a string can be encrypted very easily. We can create different types of encrypted strings in a single click. To create an encrypted string, just select any string, right click on it, and select Transform Selection.

    Encrypt a string
    1. //string: Banketeshvar Narayan  
    2. //MD5: 474a48af597980dc70d20084fec94691  
    3. //SHA-1 949089a557d1fba8efa83d1469415d69ad026e84  
    4. //SHA-256 1bf24b56917e4362a47e3ed0bc76dbf310ae8e15a58c3be752e3f7231e9bcc6b  
    5. //SHA-384 a37ef610e055bc141103793364f237b5a65e3cd7ab84d93447e37ca49fe391cc789c112b65f0d901b5de6382ac3d4c8d  
    6. //SHA-512 be27bcb24c5a6ca0cf4493f1ba8e6329aeea8f734ca81bf454ce1de2695f63833e9b39eeaaf7012ed9302fd724e30c611c0ab2ba6cdf7a41a1bd66fb457831bf  
  13. Create GUID

    In Visual studio if you would like to create a GUID you can do it very easily in a single click. Let’s see how it works. Open Visual Studio 2015—Tools Menu – Create GUID,

    Create GUID

    You can explore it with list of available GUID Format.

  14. Join Lines (Ctrl +M, J)

    Multiple lines can be joined to a single line with a single stroke of keyboard rather than organizing it manually.

    So, the below code:
    1. Person p = new Person   
    2. {  
    3.     FirstPersonName = string.Empty,  
    4.         Id = 0,  
    5.         FirstName = string.Empty,  
    6.         MiddleName = string.Empty,  
    7.         LastName = string.Empty,  
    8.         NickName = string.Empty,  
    9.         Age = 0,  
    10.         Address = string.Empty,  
    11.         City = string.Empty,  
    12.         State = string.Empty,  
    13.         ZipCode = string.Empty,  
    14.         Phone = string.Empty,  
    15.         Fax = string.Empty,  
    16.         EmailId = string.Empty,  
    17.         WhatsAPPNumber = string.Empty,  
    18.         FacebookPage = string.Empty,  
    19.         TwitterId = string.Empty,  
    20.         LinkedInAddress = string.Empty  
    21. };  
    Can be joined in one line and written like:
    1. Person p = new Person  
    2. {  
    3.     FirstPersonName = string.Empty, Id = 0, FirstName = string.Empty, MiddleName = string.Empty, LastName = string.Empty, NickName = string.Empty, Age = 0, Address = string.Empty, City = string.Empty, State = string.Empty, ZipCode = string.Empty, Phone = string.Empty, Fax = string.Empty, EmailId = string.Empty, WhatsAPPNumber = string.Empty, FacebookPage = string.Empty, TwitterId = string.Empty, LinkedInAddress = string.Empty  
    4. };  
    To join the lines, select all the lines and press Ctrl +M, J.

    Note: It is a feature of “CodeMaid Visual Extension”.


Similar Articles