← Back to C# Course | Chapter 1: Setup & Basics | Lesson 7 of 7

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:

  1. Comments
Syntax
csharp
// 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

markup
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));
    }
}
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
  1. Trying to nest /* */ comments, where the first */ ends the whole comment.
  2. Using # for comments as in Python, when C# uses // and /* */.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.