TL ; DR:
Comments are lines of code that C# will intentionally ignore (not execute). They don't do anything.
You add a single line comment in C# by typing
//followed by any text:// This is a comment! Console.WriteLine("This is not!");
Full lesson:
Comments are lines of code that C# will intentionally ignore. They don't do anything.
They're just used to create notes for yourself and others about what the code does.
There are two types of code comments in C#:
1. Single line comments:
We create single line comments by typing // and C# will ignore (not execute) any text between // and the end of the line.
Single line comments automatically end at the next line of code:
// Next line will greet the user:
Console.WriteLine("Hello user!");
Output of this code:
Hello user!
We can also use single line comments at the end of a line to explain the code:
Console.WriteLine("Hello user!"); // This line greets the user
Console.WriteLine("This is not a comment!");
Output of this code:
Hello user!
This is not a comment!
2. Multi-line comments
We can also comment multiple lines of code using multi-line comments.
We type /* to begin the comment and type */ to end the comment:
/* This is a
multi-line comment
Console.WriteLine("This will not run");
Next line will greet the user:
*/
Console.WriteLine("Hello user!");
Output of this code:
Hello user!
Assignment
Follow the Coding Tutorial and let's write some comments!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
