Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Tip/Trick

Commenting/uncommenting segments easily in C#

Rate me:
Please Sign up or sign in to vote.
4.98/5 (28 votes)
24 Jan 2011CPOL 91.9K   8   28
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)


Written By
CEO unemployable
Canada Canada
Christ Kennedy grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University. He is unemployable and currently living in Moncton, N.B. writing his next novel.

Comments and Discussions

 
GeneralReason for my vote of 5 Neat trick. Pin
Richard MacCutchan23-Jan-11 2:32
mveRichard MacCutchan23-Jan-11 2:32 
GeneralRe: thanks, I use it all the time Pin
Christ Kennedy24-Jan-11 4:50
mvaChrist Kennedy24-Jan-11 4:50 
GeneralRe: isn't it! Pin
Christ Kennedy31-Jan-11 6:50
mvaChrist Kennedy31-Jan-11 6:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.