Comment: Single Line and MultilineUse comment syntax and if-false regions to describe code. Comments do not affect runtime speed.
This page was last reviewed on Sep 14, 2022.
Comments. The C# language supports 2 comment syntax forms. Comments do not affect a program's speed. Directives can be used as comments.
Notes, compilers. Some programming languages have been affected in some way by comments in the program text. But C# is a compiled language—comments do not affect the executable.
Example comments. Here are some comments. We see the C++ style single-line comments. We see the multiline comment syntax used in C programs.
Detail For long blocks of commented text, a directive like #if-false can be used.
using System; class Program { static void Main() { // An example comment. // ... Sometimes indenting comments is helpful too. int value = 10; // Ten. /* A multi-line comment. */ value++; #if false Another way to comment, an #if false directive. #endif value *= 100; Console.WriteLine(value); } }
XML comments. These are part of the C# language specification, but not C# code. The XML comments can be parsed by a code editor (like Visual Studio) and displayed in a window.
using System; class Program { /// <summary> /// Test value and modify it. /// </summary> /// <param name="value">The important value.</param> /// <returns>The modified value.</returns> static int Test(int value) { return value * 2; } static void Main() { Console.WriteLine(Test(2)); } }
Performance, comments. When writing a program, comments do not affect the resulting program in any way. Excessive comments may make a program harder to read.
Version 1 This version of the method has no comments, and is shorter in size in the source of the program.
Version 2 This version has almost 20 lines of comments on it. Other than that, it is the same as the first version.
Result The times measured were close for both the no-comment and comment versions of the methods.
class Program { static void Increment() { int value = 0; for (int i = 0; i < 100000; i++) { for (int j = 0; j < 10; j++) { value++; } } _field = value; } static void IncrementWithComments() { // Local variable in variable slot that is incremented. int value = 0; // Loop through the first one-hundred thousand numbers. for (int i = 0; i < 100000; i++) { // Nested loop through first ten numbers. for (int j = 0; j < 10; j++) { // Increment the variable once. value++; } } // Store local variable's value in static field. _field = value; } static int _field; static void Main() { Increment(); IncrementWithComments(); } }
Increment: 0.331 ms IncrementWithComments: 0.330 ms Increment: 0.308 ms IncrementWithComments: 0.308 ms
Notes, compiler phases. Compilers are logically organized into phases, which are grouped into passes. A lexical parser converts the program text into a series of lexemes (words).
Tip At this point, the compiler can remove all lines beginning with the // characters and text between the /* and */ delimiters.
And Because of this phase, the compiled DLL from the C# code contains no trace of the comments.
Intent. It is usually recommended to describe at the level of intent, not the level of implementation. So you can comment on what the code is supposed to do.
IL Disassembler. We can see how the C# compiler translates programs into the intermediate language. IL Disassembler reads in the compiled program and shows the abstract binary format.
A summary. Comments have no effect on the performance of C# programs. There are relevant compiler concepts and style issues. A variety of syntax forms can be used.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 14, 2022 (simplify).
© 2007-2023 Sam Allen.