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

Tagged as

Commenting/uncommenting segments easily in C#

, 24 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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;


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
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.

Comments and Discussions

GeneralMy vote of 5 Pinmemberniubay31-Aug-12 4:45 
GeneralMy vote of 5 PinmemberPIEBALDconsult13-Aug-12 19:36 
GeneralMy vote of 5 PinmemberVolynsky Alex10-Jul-12 10:37 
GeneralMy vote of 3 PinmemberDonsw15-Apr-12 17:08 
GeneralRe: My vote of 3 PinmemberAndreas Gieriet3-Jun-12 6:55 
I was thinking along the same lines... (without voting down, though).
The trick is technically really neat.
From a code smell point of view, I dislike literally commented-out code.
Especially block comments are a pain, since they do not nest. E.g. if you comment out one block and you want to comment out an outer block, you maybe overlook that you have already an inner text commented out...
My hit-list is
1) remove unused code completely
2) make conditional[^] functions (comment why!), e.g. for debug mode validate and dump
3) enclose the block in question into an #if block (comment why!)
4) comment out whole blocks by prefixing with // (add //TODO to tell why - say up to where the comment goes!)
5) avoid /*...*/ comments since they do not nest
You may use logging frameworks that allow for fine grained control on what logging calls are turned on/off dynamically.
GeneralRe: yep, i think you're right. PinmemberChrist Kennedy31-Jan-11 12:19 
GeneralRe: I think this comment may have been meant for quasaron. PinmvpRichard MacCutchan31-Jan-11 8:00 
GeneralReason for my vote of 5 really helpful article Pinmembernikhi _singh23-Feb-12 0:46 
GeneralWhilst this is a useful tip in itself, isn't there a greater... PinmemberReiss17-Aug-11 0:13 
GeneralRe: forgive my slow response(s) but if by 'ship the wrong code' ... PinmemberChrist Kennedy27-Aug-11 5:09 
GeneralReason for my vote of 5 Neat! PinmemberJF201529-Mar-11 0:18 
GeneralRe: very convenient too! thanks for your vote and comment. PinmemberChrist Kennedy29-Mar-11 2:20 
GeneralThe risk area should be obvious. If you apply this method to... PinmemberTaipeiJim2-Feb-11 20:45 
GeneralRe: I'm in the habit of using this method strictly for commentin... PinmemberChrist Kennedy3-Feb-11 2:53 
GeneralReason for my vote of 5 Nice trick !! PinmemberJohnPool2-Feb-11 4:45 
GeneralRe: works pretty good! thanks. PinmemberChrist Kennedy2-Feb-11 11:23 
GeneralGood One PinmemberMember 476446731-Jan-11 22:49 
GeneralRe: nice, eh! PinmemberChrist Kennedy1-Feb-11 1:51 
GeneralReason for my vote of 5 Nice trick! Pinmemberquasaron31-Jan-11 6:12 
GeneralRe: thanks. PinmemberChrist Kennedy2-Feb-11 11:22 
GeneralReason for my vote of 5 Really useful.. PinmemberPravin Patil, Mumbai24-Jan-11 4:25 
GeneralRe: glad you agree. PinmemberChrist Kennedy24-Jan-11 5:52 
GeneralReason for my vote of 5 nice trick - have 5 PinmemberPranay Rana24-Jan-11 3:14 
GeneralRe: really handy, I often add "debug-start" & "debug-end" if tha... PinmemberChrist Kennedy24-Jan-11 5:51 
GeneralRe: great! PinmemberChrist Kennedy31-Jan-11 12:20 

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

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

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