How to write comments in C#
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;//from w w w . java2 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. jav a2 s . 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;/* w ww.ja v a2 s . c o 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.