C# Comments

In this chapter you will learn:

  1. What are C# comments
  2. What are the three types of comments
  3. Single line comments
  4. Multiple line comments
  5. XML documentation comments

Description

We use comments to explain the meaning of statements in C# code.

Three types of comments in C#

There are three styles of comments we can use in C# code.

  • Single line comments
  • Multiple line comments
  • XML documentation comments

Single line comments

A single line comments start with // and continues until the end of line.


using System;//w ww .j a  va 2 s .  c  o m

class Program
{
    static void Main(string[] args)
    {
        int i = 0;
        int j = 2;
        //This is a single line comment.
        Console.WriteLine("i=" + i);
        Console.WriteLine("j=" + j);
    }
}

The code above generates the following result.

Multiple line comments

C# multiple line comments start with /* and end with */.


using System;//from   w  w w . ja va2s.co  m

class Program
{
    static void Main(string[] args)
    {
        int i = 0;
        int j = 2;
        /*
         This is a multi-line comment.
         */
        Console.WriteLine("i=" + i);
        Console.WriteLine("j=" + j);
    }
}

The code above generates the following result.

XML documentation comments

The third type of comments is XML documentation comment. In the C# source code, the comments may contain XML tags. Those tags mark the comments to provide information for the code.


using System;//from   w  ww. j a v a 2  s .co  m

class Program
{
    /// <summary>
    /// This the XML documentation.
    /// </summary>

    static void Main(string[] args)
    {
        int i = 0;
        int j = 2;
        Console.WriteLine("i=" + i);
        Console.WriteLine("j=" + j);
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is if statement
  2. How to create if statement
  3. Example for simple if statement
  4. Combine else if clauses to test for multiple conditions
  5. Nested if statement
Home »
  C# Tutorial »
    C# Language »
      C# Statements
C# Comments
C# if statement
C# while loop
C# do while loop
C# for loops
C# foreach statement
C# switch statement
C# break statement
C# continue statement
C# goto statement
C# XML Documentation