Click here to Skip to main content
Click here to Skip to main content

Commenting/uncommenting segments easily in C#

By , 24 Jan 2011
 
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)

About the Author

Christ Kennedy
CEO unemployable
Canada Canada
Member
Christ Kennedy, published his fourth novel "Cleats of the Counter Revolution" in the summer of 2010. He grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University and is currently walking across ontario plotting a new novel, far away from any computer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberniubay31 Aug '12 - 3:45 
GeneralMy vote of 5memberPIEBALDconsult13 Aug '12 - 18:36 
GeneralMy vote of 5memberVolynsky Alex10 Jul '12 - 9:37 
GeneralMy vote of 3memberDonsw15 Apr '12 - 16:08 
GeneralRe: My vote of 3memberAndreas Gieriet3 Jun '12 - 5:55 
GeneralRe: yep, i think you're right.memberChrist Kennedy31 Jan '11 - 11:19 
yep, i think you're right.
GeneralRe: I think this comment may have been meant for quasaron. mvpRichard MacCutchan31 Jan '11 - 7:00 
GeneralReason for my vote of 5 really helpful articlemembernikhi _singh22 Feb '12 - 23:46 
GeneralWhilst this is a useful tip in itself, isn't there a greater...memberReiss16 Aug '11 - 23:13 
GeneralRe: forgive my slow response(s) but if by 'ship the wrong code' ...memberChrist Kennedy27 Aug '11 - 4:09 
GeneralReason for my vote of 5 Neat!memberJF201528 Mar '11 - 23:18 
GeneralRe: very convenient too! thanks for your vote and comment.memberChrist Kennedy29 Mar '11 - 1:20 
GeneralThe risk area should be obvious. If you apply this method to...memberTaipeiJim2 Feb '11 - 19:45 
GeneralRe: I'm in the habit of using this method strictly for commentin...memberChrist Kennedy3 Feb '11 - 1:53 
GeneralReason for my vote of 5 Nice trick !!memberJohnPool2 Feb '11 - 3:45 
GeneralRe: works pretty good! thanks.memberChrist Kennedy2 Feb '11 - 10:23 
GeneralGood OnememberMember 476446731 Jan '11 - 21:49 
GeneralRe: nice, eh!memberChrist Kennedy1 Feb '11 - 0:51 
GeneralReason for my vote of 5 Nice trick!memberquasaron31 Jan '11 - 5:12 
GeneralRe: thanks.memberChrist Kennedy2 Feb '11 - 10:22 
GeneralReason for my vote of 5 Really useful..memberPravin Patil, Mumbai24 Jan '11 - 3:25 
GeneralRe: glad you agree.memberChrist Kennedy24 Jan '11 - 4:52 
GeneralReason for my vote of 5 nice trick - have 5memberPranay Rana24 Jan '11 - 2:14 
GeneralRe: really handy, I often add "debug-start" & "debug-end" if tha...memberChrist Kennedy24 Jan '11 - 4:51 
GeneralRe: great! memberChrist Kennedy31 Jan '11 - 11:20 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 24 Jan 2011
Article Copyright 2011 by Christ Kennedy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid