Comments
Comments are notes to yourself and other readers that the computer ignores, like doodles in a notebook margin. They explain why the code does what it does.
In this page:
Syntax
// single-line comment
/* multi-line
comment */
/// <summary>
/// XML documentation comment
/// </summary>
Comments
C# supports single-line comments with //, multi-line comments with /* */, and XML documentation comments with /// that tools can turn into documentation.
Comments are ignored by the compiler and exist purely to explain code to humans. Good comments explain *why*, not what the code already makes obvious.
Note:
IDEs like Visual Studio render
/// summary comments as IntelliSense tooltips.Example: Comments
using System;
class Program
{
/// <summary>
/// Adds two integers together.
/// </summary>
static int Add(int a, int b)
{
// simple addition, no overflow checking
return a + b;
}
static void Main(string[] args)
{
/* Call the helper and print the result */
Console.WriteLine(Add(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Trying to nest
/* */comments, where the first*/ends the whole comment. - Using
#for comments as in Python, when C# uses//and/* */. - Leaving comments that describe old code after the code has changed.
Chapter Summary
- C# is a language used with .NET, which you install to build and run programs.
- The dotnet CLI creates and runs projects, starting with a Hello World program.
- Program structure, namespaces, and comments organize and document your code.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: