ASP.NET Hosting

Nested comment blocks in C#

In C#, we can use two kinds of delimiters to comment code: // and /*...*/
The first one applies to one line, the second can be applied to a set of lines or just a part of a line (block commenting). These two possibilities are useful, but I always missed the ability to nest comment blocks.

In C#, you can do the following:

/*
// this code does blabla
blabla();
// this code does yipeeyeah
yipeeyeah();
*/

But what if you want to comment out the following code?:

// this code does blabla
blabla();
/* this is a long description of the call to yipeeyeah
   that spans on multiple lines */
yipeeyeah();

In Delphi, you have three notations: // and (*...*) and {...}. This allows you to nest comment blocks.
In C#, the way you can do it is add // in front of every line (CTRL+K, CTRL+C in Visual Studio).

// // this code does blabla
// blabla();
// /* this is a long description of the call to yipeeyeah
//    that spans on multiple lines */
// yipeeyeah();

Another way of doing this is to use a #if directive. This is of course a trick, but the advantages are that your comment really looks like a block, and is collapsed by default, like a region.

#if comment // Code removed temporarily because required method not working
// this code does blabla
blabla();
/* this is a long description of the call to yipeeyeah
   that spans on multiple lines */
yipeeyeah();
#endif

No Comments