65.9K
CodeProject is changing. Read more.
Home

Commenting/uncommenting segments easily in C#

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.98/5 (27 votes)

Jan 22, 2011

CPOL
viewsIcon

100077

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;
//*/