Comments. The C# language supports 2 comment syntax forms. Comments do not affect a program's speed. Directives can be used as comments.
The C# language is compiled, which means comments do not affect the executable. It is possible to use directives like #if to insert comments, but this is not ideal.
Example comments. Here are some comments. We see the C++ style single-line comments. We see the multiline comment syntax used in C programs.
Tip 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);
}
}1100
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));
}
}4
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.
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 Jul 10, 2023 (image).