Adding comments while coding is a very good practice.
Most of the times, we end up writing very lengthy code and at that time, just to remember what a particular block of code will actually do, we use comments.
The comments in C# code also important and helpful for other team members who are helping you in building your project.
C# offers two different styles of source-code documentation: single-line comments, and multiline comments and XML Comments.
The keyboard shortcut we use for putting comments in C# code is CTRL + K + C
and to undo the comments is CTRL + K + U
.
A single-line comment begins with a double forward slash and continues until the end of the line.
For example:
int x = 7; // Comment about assigning 7 to x
Example 2:
using System;
namespace comments
{
class Program
{
static void Main(string[] args)
{
//This is a C# code comment example.
Console.WriteLine("Hello world From XDevSpace");
}
}
}
A multiline comment begins with /* and ends with */.
For example:
int x = 1; /* This is a comment that
spans two lines */
In c#, the XML Comments are defined by using ///
(triple forward slashes) and with XML formatted comment body.
For example:
///<summary>
/// This class does something.
///</summary>
public class SomeClass
{
}
The documentation comments contain XML text used to make the program documentation.
The documentation starts with three contiguous forward slashes (///
).
//...
), delimited comments (/*...*/
), and documenation comments (///
).