Thursday, 5 May 2016

Visual Studio Tips

Hi All, After long time we came up with a new post on Visual Studio IDE.

Brief on Visual Studio IDE:

Visual Studio IDE is a tool for developing  different types of applications such as Web Applications, Windows Applications, Services, Mobile applications and many more.

Visual Studio IDE tool provides several features which helps in our day to day life,

1. Remove Unnecessary namespaces in a page:

 By Default Visual Studio IDE, generates some set of code with default namespaces while creating a new page.

Scenario: If a page contains 1000 lines of code and planning to remove unused namespaces usually we do manually delete the unused namespaces and it may chance to get errors. 

Solution: Visual Studio IDE provides one of the feature to remove unused namespaces easily.
 
How to remove unused namespaces:

     1. Right Click on a page -> Select Organize using's -> Select any one among the below three options
            1.1 : Remove Unused Using's
            1.2:  Sort using's
            1.3: Remove and Sort Using's

To achieve the same above through Keyboard short cut key,

Short cut key is Shift+F10

Short Cut KeyDescription
SHIFT + F10Shows Context Menu Popup
SHIFT + F10,OShows Context Menu Popup and then Open Sub Menu
SHIFT + F10, O , RShows Context Menu Popup and then Open Sub Menu -> Select Remove Unused Using's
SHIFT + F10, O, UShows Context Menu Popup and then Open Sub Menu -> Select Sort Using's
SHIFT + F10 + O + aShows Context Menu Popup and then Open Sub Menu -> Select Remove and Sort Using's

While applying the above feature and there is no impact on Unused using's at application level it's just ignore the unused using's which gives code cleaner and maintainable.

Advantages of removing Unused Namespaces

i). Cleaner code with limited namespaces which are used in a page and also easy to readable and maintain.

ii). Compilation should be very fast because checks only fewer namespaces.

iii) Intelligence runs faster the reason is before removing unused namespaces it loads all of the classes and properties by that time difficult to select among many. After removing there are very limited by this way Intelligence works fast and easy to select by developer.

2. How to know the class belongs to which namespace?

     Developer want to know the class belongs to which namespace.

Ex: SqlConnection objCon=new SqlConnection();

If we want to know the above class in which namespace there are two ways to know the namespace

 i) Short Cut: CTRL+.(dot) 

ii) Hover on class and it shows a down arrow symbol -> Select namespace which showing like this "Using System.Data.SqlClient".

In order to achieve this feature, class should be follow the two rules

Rules:
     1. The classes should be case-sensitive
          Ex:  SqlConnection objCon=new SqlConnection(); - Correct
                  sqlconnection objCon=new sqlconnection(); - Wrong
     2. Before knowing the class namespace that namespace should be there in Reference folder then only the context menu shows otherwise it shows different options like create a new class/type.

3. KeyBoard Shortcuts

The below shortcuts are very useful in our daily based tasks,

Short Cut KeyDescription
CTRL +K DTo Format document
CTRL+K CTo Comment the Selected Code
CTRL+K UTo UnComment the Selected Code
CTRL+SPACEFor Intelligence
ALT+SHIFT+ENTERToggle Full Screen IN/OUT
SHIFT+F6Build Project
CTRL+SHIFT+B  OR F6Build Solution
CTRL+ALT+PAttach the debugger to a process
CTRL+M MExpand or Collapse current element
CTRL+M,OCollapse ALL
CTRL+M,LToggle all Outlining
CTRL+SHIFT+UConvert selected text to UPPERCASE
CTRL+UConvert selected text to LOWERCASE
Where Solution contains class libraries, web application projects. If you want to build project alone Press SHIFT+F6 or solution:F6

4. Command Window in Visual Studio
       
       Is one of the feature provided in Visual Studio IDE. We can run the commands in Command Window like the same way we do with IDE.

Short Cut to open Command Window: CTRL+W,A

Ex: If you want to open a file through command window

> File.OpenFile c:\Projects\Program.cs
Or
>of c:\Projects\Program.cs

To clear all commands in Command Window
>cls

5. Watch Window

         Watch window is a very useful debugging feature for developers to edit the value and re-evaluate and also calculate expression while debugging time and also change the value for a variable and continue the execution.

Note: Watch Window executes and shows only at debug time.

Where to see Watch Window:

DEBUG -> Windows -> Watch -Watch1

Short Cut Key : CTRL+D, CTRL+W

I am hoping that this post is very helpful and useful to all who are often working with Visual Studio IDE for their development.

We are expecting your valuable comments.

Happy Coding :)