C# Comments

Comments are notes written inside C# source code for developers to read. The compiler ignores regular comments, so they do not change the program output or execution.

C# supports the following comment styles:

  • Single-line comments using //
  • Multi-line comments using /* and */
  • XML documentation comments using ///

C# Single-Line Comments with //

Two forward slashes // start a single-line comment. Everything from // to the end of that line is treated as a comment. A single-line comment can appear on its own line or after a C# statement.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            //this is a single line comment
            Console.WriteLine("Hello TutorialKart"); //this is also a comment
        }
    }
}

Output

Hello TutorialKart

The first comment occupies a complete line. The second comment follows a statement. Neither comment appears in the output.

Commenting Out One C# Statement

Placing // before a statement prevents that statement from being compiled and executed. This is commonly called commenting out code.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            //Console.WriteLine("Hello World");
            Console.WriteLine("Hello TutorialKart");
        }
    }
}

Output

Hello TutorialKart

The first Console.WriteLine statement is commented out, so only the second statement runs.

C# Multi-Line Comments with /* */

A multi-line comment starts with /* and ends with */. It may span several lines, although it can also be written on one line.

</>
Copy
/* Comment text */

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            /*  This is
                a multi-line
                comment */
            Console.WriteLine("Hello TutorialKart");
        }
    }
}

Output

Hello TutorialKart

The text between /* and */ is ignored. Execution continues with the Console.WriteLine statement.

Commenting Out Multiple C# Statements

A block comment can temporarily disable several consecutive statements.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Hello TutorialKart");
            /* Console.WriteLine("Hello World");
            Console.WriteLine("Hello User"); */
        }
    }
}

Output

Hello TutorialKart

The two statements inside the multi-line comment are not compiled, so only Hello TutorialKart is printed.

Nested Multi-Line Comments Are Not Supported

C# block comments cannot be nested. The first */ closes the comment, even when another /* appears inside it. Use separate // comments when the code already contains a block comment.

</>
Copy
// Temporarily disabled code:
// Console.WriteLine("First line");
// /* Existing block comment */
// Console.WriteLine("Second line");

C# XML Documentation Comments with ///

Three forward slashes /// create an XML documentation comment. These comments describe classes, methods, properties, parameters, return values, and other code elements. Development tools can display this information through IntelliSense, and the compiler can generate an XML documentation file.

</>
Copy
/// <summary>
/// Adds two integer values.
/// </summary>
/// <param name="first">The first integer.</param>
/// <param name="second">The second integer.</param>
/// <returns>The sum of the two integers.</returns>
static int Add(int first, int second)
{
    return first + second;
}

Unlike ordinary comments, documentation comments have a structured purpose. Common XML elements include <summary>, <param>, <returns>, <remarks>, and <exception>.

Difference Between C# Comment Types

Comment typeSyntaxTypical use
Single-line comment// commentShort notes and temporary single-line code removal
Multi-line comment/* comment */Long explanations or temporarily disabling a block of code
XML documentation comment/// commentDocumenting public classes, methods, properties, parameters, and return values

Writing Useful Comments in C# Code

Useful comments explain intent, constraints, or decisions that are not obvious from the code itself. Avoid comments that simply repeat what a clearly named statement already says.

  • Explain why a non-obvious approach is required.
  • Document assumptions, edge cases, and external constraints.
  • Keep comments accurate when the related code changes.
  • Use clear method and variable names instead of relying on comments to explain confusing code.
  • Use XML documentation comments for APIs and reusable members.
  • Remove obsolete commented-out code when version control already preserves its history.

For example, the following comment explains the reason for a value rather than repeating the assignment:

</>
Copy
// Keep retries low because each attempt calls a rate-limited service.
int maximumRetries = 3;

C# Comments: Common Questions

Do C# comments affect program performance?

No. Regular comments are ignored during compilation and do not become executable instructions.

Can a C# comment appear after a statement?

Yes. A // comment may follow a statement on the same line. Everything after // is treated as comment text.

Can C# multi-line comments be nested?

No. C# does not support nested /* */ comments. The first closing delimiter ends the comment.

What is the difference between // and /// in C#?

// creates an ordinary single-line comment. /// creates an XML documentation comment associated with the code element that follows it.

C# Comments Summary

Use // for single-line comments, /* */ for comments that span one or more lines, and /// for XML documentation. Comments should clarify intent and constraints without duplicating information already expressed clearly by the code.

In this C# Tutorial, we learned how to write single-line, multi-line, and XML documentation comments in C# and how to use them in different scenarios.