C# Comments
Comments are an essential part of the code. They allow developers to explain their code, document important information, and make the code easier to understand. Whatever you write in comments Compilers ignore it and do not execute them.
In C#, There are three types of comments which are defined below
- Single-line comments
- Multi-line Comments
- XML comments
Comments are non-executable code lines ignored by the compiler. They offer explanations and context for developers, enhancing readability in complex projects.
Note: For writing good and productive comments please remember the points mentioned below:
- Comments are self-explanatory. Comments should add value by explaining complex or non-obvious logic.
- Comments should be clear and concise. Avoid writing overly complex comments that could confuse the reader.
1. Single-Line comments
A single-line comment in C# is used to add a brief explanation or note about a specific line of code. This type of comment starts with two forward slashes // and continues until the end of the line.
Syntax:
// This is a single-line comment which used to explain the code
Example:
// Using single-line comment
using System;
public class Geeks
{
public static void Main()
{
// This is a single-line comment explaining the next
// line of code
// This is not going to interrupt the flow of the
// program.
int number = 300;
// Assigning 10 to the variable number
Console.WriteLine(number);
// Printing the value of number
}
}
Output
300
2. Multi-Line Comments
A multi-line comment is used to comment out a block of code that spans multiple lines. begins with /* and ends with */. Everything between these symbols is considered part of the comment. And not executed by the compiler.
Syntax:
/*
This is a multi-line comment.int number = 20;
Console.WriteLine(“this is alternative code”);It can span multiple lines.
Each line within the block is part of the comment.*/
Example:
// Using multi-line comments
using System;
public class Geeks
{
/*
This block of code does the following:
- Initializes an integer variable.
- Prints the value of the variable.
*/
public static void Main()
{
int number = 150;
Console.WriteLine(number);
}
}
Output
20
3. XML Comments
It is a special type of comment in C# and is used to create the documentation of C# code by adding XML elements in the source code. XML elements are added in XML Documentation Comments of C#. They are written using XML tags and begin with ///
Note: These comments are used to describe the purpose and functionality of methods, classes, properties, and other members of your code.
Syntax:
/// <summary>
/// This method adds two numbers and returns the result.
/// </summary>
/// <param name=”a”>The first number to add.</param>
/// <param name=”b”>The second number to add.</param>
/// <returns>The sum of the two numbers.</returns>public int Add(int a, int b)
{
return a + b;
}
Example:
// Using xml comments
using System;
public class Geeks
{
/// <summary>
/// Adds two numbers and returns the result
/// </summary>
/// <param name="x">The first number to add</param>
/// <param name="y">The second number to add</param>
/// <returns>The sum of x and y</returns>
public int Add(int x, int y) {
return x + y;
}
public static void Main()
{
Geeks calc = new Geeks();
Console.WriteLine(calc.Add(8, 8));
}
}
Output
16