C# Code Comments

Hello and welcome to this tutorial. In this tutorial we will explore c# code comments and how we can use them to improve the readability and the understanding of your code.

The c# code comments (document xml comment) can be added to the code so it can be seen as a tool tip in the code editor. The comments can provide a valuable reference to others and your self in what this code can doe, what type of values it returns and what possible exceptions it will throw. And many other useful features. So let’s get started:

 

<summary>:

A well known code  comments that can be used to describe your function or property so as soon as you type it’s name in the code editor. To do, at the top of your method add the summary tag as follow:

 

Now, in the editor when you hover over the method or type it’s name you will see the summary tag content as follow:

 

summary-csharp-code

summary-csharp-code

<code>:

The code tag will referee to a code that can be written inside your code or as an example. The following are example of the code tag:

 

<example>:

The example tag is to describe an example code usage in the code as follow:

 

<see cref=””/>:

The see cref (cref shorts for “code reference”) aims to give a reference to another code (class or function or others) as a reference when you write code. Inside the cref attribute we place the name of the class or method we want to reference:

And in the IntelliSense you can see the name of the referenced method colored:

see cref c# code comments

see cref c# code comments

<exception>:

The exception tag shows what type of exceptions the method or class called that might happen:

And in the IntelliSense you can view what exceptions the class or method can throw:

exception comment c#

exception comment c#

Also, you can add multiple exceptions to the comments:

 

<list>:

The list allow to format and add a new line for the code in the IntelliSense. So the code comment looks much better and neater. There is three types of list: bullet, number, table. Inside the list you need to define each item you want:

So the list looks like this in the code editor:

list c# comment

list c# comment

<param>:

The parameter attribute allows you to describe each parameter in the function and what you need to pass for more information:

When you type in the method, the IntelliSense will show you the description for each parameter as follow:

parameter c# comment

parameter c# comment

 

<remarks>:

The remarks comment provide additional information about the class or method. The summary is intended for the main/primary information and the remarks is for the secondary information.

 

And in the code editor the remarks will be shown after the summary:

 remarks c# comment


remarks c# comment

<returns>:

The returns shows what values will be returned by the methods:

 

 

I hope you find this tutorial helpful 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *