Comments in C#

Comments in C# are used to document and explain code, making it more readable and maintainable. The C# compiler ignores all comments during compilation, so they don't affect the program's performance. C# supports three types of comments: single-line, multi-line, and XML documentation comments.

Syntax

Following is the syntax for single-line comments −

// This is a single-line comment

Following is the syntax for multi-line comments −

/* This is a multi-line comment
   that spans multiple lines */

Following is the syntax for XML documentation comments −

/// <summary>
/// This method performs a specific task
/// </summary>

Single-Line Comments

Single-line comments begin with two forward slashes (//) and continue to the end of the line. They are perfect for brief explanations or quick notes −

using System;

class Program {
    static void Main() {
        // Declare and initialize a variable
        int age = 25;
        
        // Display the value
        Console.WriteLine("Age: " + age); // Inline comment
        
        // TODO: Add validation for negative values
    }
}

The output of the above code is −

Age: 25

Multi-Line Comments

Multi-line comments start with /* and end with */. They can span multiple lines and are useful for detailed explanations or temporarily disabling code blocks −

using System;

class Calculator {
    static void Main() {
        /* 
         * This is a simple calculator program
         * Author: Developer Name
         * Created: 2024
         */
        
        int a = 10;
        int b = 5;
        
        /* Calculate different operations
           and display the results */
        Console.WriteLine("Addition: " + (a + b));
        Console.WriteLine("Multiplication: " + (a * b));
        
        /* 
        Console.WriteLine("This code is commented out");
        Console.WriteLine("It won't execute");
        */
    }
}

The output of the above code is −

Addition: 15
Multiplication: 50

XML Documentation Comments

XML documentation comments start with three forward slashes (///) and are used to generate documentation. They provide structured information about methods, classes, and parameters −

using System;

class MathOperations {
    /// <summary>
    /// Calculates the area of a rectangle
    /// </summary>
    /// <param name="length">The length of the rectangle</param>
    /// <param name="width">The width of the rectangle</param>
    /// <returns>The area as a double value</returns>
    public static double CalculateArea(double length, double width) {
        return length * width;
    }
    
    static void Main() {
        double area = CalculateArea(5.0, 3.0);
        Console.WriteLine("Rectangle area: " + area);
    }
}

The output of the above code is −

Rectangle area: 15

Best Practices for Comments

Comment Type Best Used For Example
Single-line (//) Brief explanations, inline notes // Calculate tax amount
Multi-line (/* */) Block explanations, disabling code /* Complex algorithm explanation */
XML (///) Method documentation, API docs /// <summary>Method description</summary>

Conclusion

Comments in C# are essential for code documentation and maintenance. Use single-line comments for quick explanations, multi-line comments for detailed descriptions or code blocks, and XML documentation comments for generating professional API documentation.

Updated on: 2026-03-17T07:04:35+05:30

335 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements