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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jul 10, 2023 (image).