Hello everyone, I hope you all are fine and having fun. Today’s tutorial is a quick one and is about how to use C# Comments. Comment is an important part of any programming language, although its not a directly part but they indirectly help a lot especially in complex projects.
I have also shared a tutorial on How to use comments in c++, you should read that post as well because I have explained few basic concepts in it. You guys are also welcome to give us feedback about this new C# tutorial series. If you have any suggestions about it then do let us know in comments.
How to use C# Comments ???
- Comments are simple documented text lines, completely ignored by the compiler and are used to explain or provide any additional information about coding.
- There are 3 types of comments available in C#, which are:
- Single Line Comments. ( Symbol used: // )
- Multi-Line Comments. ( Initial Symbol: /* Closing Symbol */ )
- XML Documentation Comments. ( /// )
- Now, let’s have have look at each one of them in detail:
Single Line Comments
- Single Line Comments are used to comment any text in a single line, we have to place two forward slashes ( // ) and everything after that will be ignored by the compiler.
- Let’s have a look at How to use Single Line Comments, in below figure:
- You can see in above figure, that I have used green comments above each block to explain my code i.e. mentioning 2D & 3D arrays, accessing array elements etc.
- Moreover, I have also used comments in front of the line i.e. displaying data at location 0, 0 etc. In this case, I am just explaining a single line.
Multi-Line Comments in C#
- We can also comment out multiple lines by using /* */ these symbols.
- Let’s say, you don’t want to execute some part of your code i.e. 100 lines, then it would be too difficult to place // these symbols in front of each line.
- In such cases, there’s a need for Multi-Line Comments, here’s an example:
- At the top of above code, you can see I have used Multi-Line Comments to give a simple introduction, here you can also add description of your project/code.
- Moreover, I have also commented out Console code, I just want output using For Loop and I have used Multi-Line Comments for that.
XML Documentation Comments
- In Microsoft Visual Studio, when you hover your cursor on any object then it pops up an info box.
- If you want to add any information in this pop up box then you need to use XML Documentation Comments, as shown in below figure:
- In the above code, I have created a new class named TEPClass, we will discuss classes later in detail, but for now just focus on XML comment above that class.
- In Program class, I have called TEPClass and now when I hover my cursor over it, you can see a pop up box appeared and it has that XML comment in it.
- I have placed a red arrow in front of it, here we can place a short description of our class.
Why we need to use C# comments ???
- Let’s say you have to use some old code which you have designed a year ago.
- Now when you read your own code after a year, it will be difficult to understand.
- But if you have placed some check note (comments) for each command then you can easily understand it.
That’s all for C# comments. I hope you guys have understood how & why we need to use C# Comments. In next tutorial, we will have a look at How to use If Loop in C#.