Lesson 5 +10 XP

C# Comments

C# Comments

Comments are notes you leave in your code. The compiler ignores them - they're only for humans.

Single-line comments

Use two forward slashes //:

// This is a comment
Console.WriteLine("Hello World!");

A single-line comment can also sit at the end of a line of code:

Console.WriteLine("Hello World!");  // This is a comment

Multi-line comments

Use / ... / to comment out several lines at once:

/* The code below will print
the words Hello World
to the screen */
Console.WriteLine("Hello World!");

XML documentation comments

C# also supports XML documentation comments starting with ///:

/// <summary>
/// Adds two numbers.
/// </summary>
int Add(int a, int b) => a + b;

These can be turned into readable API documentation and give intellisense hints in your editor.

Why comment?

  • Explain what tricky code does.
  • Note why a decision was made.
  • Temporarily disable code while debugging.

TL;DR

  • // for single-line comments.
  • / ... / for multi-line comments.
  • /// starts an XML documentation comment.
  • Comments never run - they're for humans.