-
C# Comments
-
In C#, comments are used to explain the code and make it more understandable, it is required most of the times because code base is maintained by team of developers who may not always work on only one module, these comments are required so that other developers in the team can understand the code (variable declaration, logic implemented to fix any issue, etc..)
These comments are ignored by the compiler and do not affect the execution of the program. There are three types of comments in C#:1. Single-line Comments
Single-line comments start with two forward slashes (
Example://
). Everything after//
on that line is considered a comment.
// This is a single-line comment int x = 5; // This is an inline comment
2. Multi-line Comments
Multi-line comments start with
Example:/*
and end with*/
. Everything between/*
and*/
is considered a comment. Using this we can make whole paragraph or big length of text as a comment. Short cut for this – first select the code or text you want to comment out then pressctrl
+k
+c
together.
/* This is a multi-line comment. It can span across multiple lines. */ int y = 10;
3. XML Documentation Comments
The XML documentation comments are used to generate external documentation or used to build API documentation which is readable by external tools. They start with
Example:///
and are typically used above classes, methods, properties, etc. These comments are also used by tools like Visual Studio to display information about the code. IntelliSense also read these comments, and uses the contents to show the docs for your code in the assistance tooltips as you type.
/// <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 a and b</returns> public int Add(int a, int b) { return a + b; }
//
: Single-line comment/* */
: Multi-line comment///
: XML Documentation comment