A comment within C# is an area where we can add text to explain what our code is doing. It allows us to better understand a piece of code quickly without having to read it all through and figure it out.

Comments are ignored by the computer so you don’t have to worry about them popping up on the console.

With comments in C# there are two main types that you will see.

Type 1 is a single line comment demonstrated through the // at the beginning:

//prints Hello World 
Console.WriteLine("Hello World");

This type of comment starts with a //. Wherever you place the // the computer will ignore anything after the slashes on that line.

The 2nd type of comment is a multi line comment.

This allows a comment to span over multiple lines until you close it.

Here’s an example:

/* This piece of code will print Hello World 
 * It is written in C#
 */

    Console.WriteLine("Hello World");

The multi line comment is opened by a /* and is closed a */.

Anything that’s written between the open and close, no matter how many lines, will be classified as a comment for the computer and it will not be run.

Comment any questions below!


Discover more from

Subscribe to get the latest posts sent to your email.

Leave a comment

Trending