C# Comments – Asterisks in Multi-line Comments

ccommentsprogramming practicesvisual studio 2012

Using Visual Studio 2012 with C#, when I write multi-line comments, VS automatically begins each line with an asterisk, like this:

/*
 *
 *
 */

I was just curious if this was the case in programming languages other than C#, or if there was a specific reason for this template.

Best Answer

Some of the first comment syntax introduced into programming where statement comments. These kinds of comments required their own line.

Such as in basic

REM This is a comment.

This created longer source code and used a keyword. Around the same time came end-of-line comments that requires only one character to start the comment.

Such as Fortran or Assembly

MOV C,1 ; This is a comment

For languages that had simple grammar this was fine, but as more advanced grammar was developed two characters were used to start a comment.

Such as in C++

int c = 1; // This is a comment

Or such as SQL

SELECT * FROM x -- This is a comment

Many languages supported block comments in addition to end-of-line comments. These languages often supported the use of math operators *+/ to write expressions.

A block comment can not use the same grammar syntax used for code blocks. For example, if { } is used to block code then it can't be reused for comments. The same is true for () brackets since they are used to order math expressions. So often two characters were used to open/close the block comment.

We take syntax highlighting of today's IDEs for granted. When block comments were introduced to programming there was no syntax highlighting. Someone could easily introduce a bug if block comments were too close to math expressions.

For example; If you used (- this is a comment -) to mark blocks, then you could easily write a math expression as;

int x = 1+(-10);
int y = 2+(z-);

The above would actually compile to int x = 1+;

These kinds of problems actually do exist in some older languages. As a result the use of /* this is a comment */ for multi-line comments became very popular. The reason is that visually /* is not a valid math operations.

For example;

int x = 1 /* 8;
int y = 3 */;

To a human reader there is no mistake. It's not valid math. You can still read the comment inside the expression. StackExchange is adding syntax highlight (lol) but picture it without and colors.

This became a standard comment block because it solved many grammar problems in parsing the language while remaining easily identifiable to humans as a comment.

When VS adds an extra * to each line. It's to ensure readability of the comment when there is no syntax highlighting. If you open the source code in a regular text editor, then those lines are clearly shown as a comment.

If the start of a comment /* scrolls above the screen. There is no way to know if what you're reading is a comment or not. Example; someone blocked out a chunk of code.

I was just curious if this was the case in programming languages other than C#, or if there was a specific reason for this template.

The specific reason is human readability, and has nothing to do with the programming language itself. For long comments that may scroll off screen it allows the reader to understand the text is a comment.

Any character would suffice as this example;

/*
 # Still a comment.
 # Still readable if very long.
 #
 #
 */

Visual Studio will automatically add * characters to multi-line comments. This is a configurable options in the IDE.

It should be noted that other popular IDEs also offer this feature. I've seen it in Eclipse, WebStorm, IntelliJ and NetBeans. Those editors will add new lines to comment blocks while you type for languaging such as C++, Java and PHP.

I've never seen this done for XML or HTML block comments in any editor. It appears to be a common feature for C style languages.