</>
Skip to content
C# lessons (6/34)

C# — Comments

Single-line comments

using System;

// This is a single-line comment
int x = 10; // Inline comment

Multi-line comments

using System;

/*
  This block spans multiple lines.
  Use it when a single line isn't enough.
*/
int result = 42;

XML documentation comments

C# uses /// for documentation that IDEs can parse:

/// <summary>
/// Calculates the total price with tax.
/// </summary>
/// <param name="price">The base price</param>
/// <param name="taxRate">The tax rate as a decimal</param>
/// <returns>The total price with tax applied</returns>
public static double CalculateTotal(double price, double taxRate)
{
    return price * (1 + taxRate);
}

XML comment tags

TagPurpose
<summary>Brief description
<param>Parameter documentation
<returns>Return value
<remarks>Additional details
<example>Usage example
<exception>Possible exceptions
<see>Cross-reference
/// <summary>
/// Represents a 2D point in Cartesian coordinates.
/// </summary>
/// <remarks>
/// This struct is immutable. Use the constructor to create new points.
/// </remarks>
/// <example>
/// var p = new Point(3.0, 4.0);
/// double dist = p.DistanceTo(origin);
/// </example>
public struct Point
{
    public double X { get; }
    public double Y { get; }

    public Point(double x, double y) { X = x; Y = y; }

    /// <summary>
    /// Calculates the Euclidean distance to another point.
    /// </summary>
    /// <param name="other">The other point</param>
    /// <returns>The distance between the two points</returns>
    public double DistanceTo(Point other)
    {
        double dx = X - other.X;
        double dy = Y - other.Y;
        return Math.Sqrt(dx * dx + dy * dy);
    }
}

Pragma warning

// Disable specific warnings
#pragma warning disable CS0618
// Code using obsolete API here
#pragma warning restore CS0618

When to comment

  • Explain why something is done, not what
  • Mark TODOs and FIXMEs
  • Document public APIs with XML comments
  • Keep comments near their code

Mini Practice

Write C# code that:

  1. Uses XML comments for a class and its methods
  2. Includes a <remarks> tag with additional context
  3. Writes inline comments explaining complex logic

Up Next

In the next lesson, you'll learn about Data Types — value and reference types in C#.

Related Topics

Frequently Asked Questions about Comments

What is Comments in C#?

Comments is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Comments?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Comments.

Why is Comments important in C#?

Comments is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.