Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Commenting/uncommenting segments easily in C#

4.98/5 (28 votes)
24 Jan 2011CPOL 100.1K  
Comment out a large segment by changing a single line
You're probably already aware of the two popular types of ways to comment-out text in your code:
1. Single line comment with the double right-slash:
// this is an example of a single line of comment

2. Commenting out an entire segment with the opening /* and the closing */:
/*
int intValueA = 12;
int intValueB = 234;
int intSum = intValueA + intValueB;
*/

and you've probably commented out an entire segment with no regard for the single line comments tucked within:
/*
// set first value
int intValueA = 12;
// set second value
int intValueB = 234;
// calculate sum
int intSum = intValueA + intValueB;
*/

but have you ever considered commenting out the opening/closing comments? "why would I do that?" you may ask.
///*
// set first value
int intValueA = 12;
// set second value
int intValueB = 234;
// calculate sum
int intSum = intValueA + intValueB;
//*/


Of course when you comment out the opening-comment slash-asterisk, then that segment of code is no-longer commented out and by commenting out the closing asterisk-slash combination, it is ignored whenever the segment is not commented it and it still terminates the segment if the opening is not commented.
In other words, by doing this, you can easily comment/uncomment the entire segment by uncommenting/commenting the opening comment.

/*  <- single line commenting this line 'uncomments' the entire segment!
// set first value
int intValueA = 12;
// set second value
int intValueB = 234;
// calculate sum
int intSum = intValueA + intValueB;
//*/

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)