Select Page

Just like with any other programming language, it’s pretty easy to write comments in C#. When you write comments, it really just means that you’re writing something in your files that aren’t part of the code. Developers often use comments to annotate their code and explain certain functions or functionalities that they have created.

This is super helpful if you’re writing open source code and want to help other developers who may be using your code in the future. Comments can also be used even if you’re not writing open source code, however, and are a good way to stay organized or perhaps write small reminders for yourself about how something works or the purpose of a certain code block.

In C#, there are two different ways to write comments. One involves writing comments that only span the length of one single line, and the other involves writing multi-line comments. If you’re familiar with CSS or any other OOP language the comment system for C# is practically the same.

For single liners, you need to preface your comment with two forward slashes “//”, like this:

i = 0
while(i<4){
   Console.WriteLine(i);
   //add 1 to the variable i
   i++;
}

The sentence in the example above “add 1 to the variable i” is a comment, and you can tell this because of the two forward slashes that precede it.

If you want to write one that takes up multiple lines in your file, you’ll have to enclose the comment in forwarding slashes and asterisks, like this: “/* insert comment here */”. See below for an example:

i = 0
while(i<4){
   Console.WriteLine(i);
   /* for this example, the comment is going to span two lines of this file and is therefore a multi-line comment */
   i++;
}

As long as your multi-line comment remains within the two /* and */ symbols, you can have your comment span as many lines as you like.

Share This