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.
- 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.
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.
- 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.
To know more about Bing Contextual Search, go through the following link:
Use Developer Assistant Extension with Visual Studio 2015
- Rename…
To rename a method, property, variable, or type use the keyboard shortcut: F2
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.
- 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.
- 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
- Quick Actions and Refactorings…
The shortcut for “Quick Actions and Refactorings…” is Ctrl + Alt + . Or Ctrl + .
Example 1:
As you can see in the above image, it is prompting for removing one parameter which is not being used.
Example 2:
Example 3:
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
- 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.
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.
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.
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.
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
- 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.
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.
- Encode/Decode Selection
In Visual Studio piece of code can be encoded very easily.
So, HTML Encode for “<b>Banketeshvar Narayan<b>” will be “<b>Banketeshvar Narayan<b>”
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.
- 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,
- Remove Diacritics
Diacritics can be removed very easily from a string, as you can see in the below screenshot:
Select the string – Right click – Transform selection – Remove Diacritics. So, text “diakritikós” will be converted to “diakritikos”.
- 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.
- 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,
You can explore it with list of available GUID Format.
- 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:
- Person p = new Person
- {
- 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
- };
Can be joined in one line and written like:
- Person p = new Person
- {
- 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
- };
To join the lines, select all the lines and press Ctrl +M, J.
Note: It is a feature of “CodeMaid Visual Extension”.