Visual Studio Tips And Tricks Series - Part One

Datetime:2016-08-23 04:26:57          Topic: Visual Studio  XML           Share

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
class Program  
{  
//This is an example of Auto Property Initializer in C# 6  
public string UserName { get; } = "Banketeshvar Narayan Sharma";  
}  
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:
//public string ModelId { get; set; }   
//public string Height { get; set; }   
//public string Width { get; set; }   
//public string Thickness { get; set; }  
//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.
Use /*…*/ for multiple lines  
/* This is an example of Dictionary initializer in C# 6 
Where as in C# 5 we have to write like 
{ 101,"Banketeshvar Narayan Sharma"}, 
{ 102,"Rajnish Kumar Choudhary"}, 
{ 103,"Rajnish Kumar Choudhary"} 
*/  
public Dictionary<int, string> _students { get; } = new Dictionary<int, string>()  
{  
[101] = "Banketeshvar Narayan Sharma",  
[102] = "Rajnish Kumar Choudhary",  
[103] = "Bhupesh"  
};  
  
Use /*…*/ for single line  
public string ModelId { get; set; } /* = string.Empty; */  
Use /*…*/ for commenting in middle of a line  
public string ModelId /*{ get; set; }*/ = string.Empty;  
XML Documentation Comments
/// <summary>  
///   
/// </summary>  
/// <param name="emailId"></param>  
/// <param name="subject"></param>  
/// <param name="msgBody"></param>  
/// <param name="listOfCcEmailIds"></param>  
/// <param name="listofBccEmailIds"></param>  
public void SendMail(string emailId, string subject, string msgBody, string[] listOfCcEmailIds, string[] listofBccEmailIds)   
{  
    //send mail.  
}  
To write the above xml comment just type “///” and press enter it to automatically generate these codes:
/// <summary>  
///   
/// </summary>  
/// <param name="emailId"></param>  
/// <param name="subject"></param>  
/// <param name="msgBody"></param>  
/// <param name="listOfCcEmailIds"></param>  
/// <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
public int Id { get; set; }  
  
/// <summary>  
/// Gets or sets the first name.  
/// </summary>  
/// <value>  
/// The first name.  
/// </value>  
public int FirstName { get; set; }  
  
public int MiddleName { get; set; }  
  
public int LastName { get; set; }  
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.
/// <exclude />  
public int FirstName { get; set; }  
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
And inside GhostDoc
You can find GhostDoc inside Tools Menu.




About List