Comments in C - potatoscript/csharp GitHub Wiki
In programming, we use comments to write notes or explanations in the code. Think of them like post-it notes you stick on important parts of your homework or projects to remind yourself of something. These notes do not affect how the program works. Theyβre just there to help you (and other programmers) understand what the code does.
You can write comments anywhere in your code, and they will be ignored when the program runs. Itβs like writing on paper but not showing it to the teacher. βοΈπ
Comments help programmers:
- Understand the code.
- Remember what they were thinking while writing it.
- Explain the code to others.
- Troubleshoot and debug if things go wrong.
Imagine you're building a LEGO castle and you leave comments like βThis part is for the towerβ to help you remember what each piece does when you come back to it later. π°
There are two types of comments youβll use in C#:
- Single-line comments π
- Multi-line comments π
Single-line comments are used to explain one specific line of code. You can write a single comment at the end of a line of code or on its own line.
To create a single-line comment, you use two forward slashes //
at the start of the comment.
// This is a single-line comment. It helps explain the code below.
int age = 10; // This is where we store Cody's age.
If you want to explain a longer part of your code or make multiple notes, you can use multi-line comments. These comments are written over several lines.
To create a multi-line comment, you start with /*
and end with */
.
/*
This is a multi-line comment.
It can be used when you need to explain
a larger piece of code or write detailed notes.
*/
int age = 10;
In C#, there are even cooler ways to structure your comments and code:
#region
and #endregion
allow you to group related code and collapse it in the IDE (like Visual Studio). It's like putting different parts of your homework in neat folders so you can focus on whatβs important. π
- Makes your code clean and organized.
- Helps group related methods or functions together.
- You can collapse or expand code to hide or show sections.
using System;
class Program
{
static void Main()
{
#region Dog Information
// Max is a dog. Let's store some information about Max.
string dogName = "Max"; // Max's name
int dogAge = 5; // Max is 5 years old
string favoriteToy = "Ball"; // Max's favorite toy
#endregion
#region Display Dog Info
// Display Max's information
Console.WriteLine("Dog's Name: " + dogName);
Console.WriteLine("Dog's Age: " + dogAge + " years old");
Console.WriteLine("Dog's Favorite Toy: " + favoriteToy);
#endregion
}
}
π Whatβs happening?
-
#region Dog Information
groups the code related to storing Maxβs information. -
#region Display Dog Info
groups the code that displays Maxβs information. - You can collapse and expand these sections in the IDE to make your code easier to navigate.
XML comments start with ///
and are used to create documentation for your methods and classes. This is like adding a label to each section of your homework that explains whatβs inside. π
- Provides clear documentation for methods and classes.
- Helps other developers (and your future self) understand what each method does.
- When you hover over a method in Visual Studio, it shows the summary.
using System;
class Program
{
/// <summary>
/// Main method where the program starts.
/// </summary>
static void Main()
{
// Call the method to display dog info
DisplayDogInfo("Max", 5, "Ball");
}
/// <summary>
/// Displays information about a dog.
/// </summary>
/// <param name="name">The dog's name.</param>
/// <param name="age">The dog's age.</param>
/// <param name="toy">The dog's favorite toy.</param>
static void DisplayDogInfo(string name, int age, string toy)
{
Console.WriteLine("Dog's Name: " + name);
Console.WriteLine("Dog's Age: " + age + " years old");
Console.WriteLine("Dog's Favorite Toy: " + toy);
}
}
π Whatβs happening?
-
/// <summary>
describes what the method does. -
/// <param>
explains the parameters passed to the method. - When you hover over the method name in Visual Studio, the summary pops up.
- You have many methods or blocks of code that need grouping.
- You want to collapse/expand sections for easy navigation.
- You are documenting classes, methods, or properties.
- You want to generate API documentation for your code.
- Use comments sparingly: Only comment on tricky or complex parts of the code.
-
Group related code: Use
#region
to organize sections and keep code clean. -
Document important methods: Use
/// <summary>
to make your code self-explanatory.
Hereβs a fun practice example that uses comments, regions, and XML documentation to organize and explain a program about a cat named Luna. πΎ
using System;
class Program
{
/// <summary>
/// Main method where the program starts.
/// </summary>
static void Main()
{
#region Cat Information
// Storing information about Luna the cat
string catName = "Luna"; // Cat's name
int catAge = 3; // Cat's age
string favoriteToy = "Yarn"; // Luna loves playing with yarn
#endregion
#region Display Cat Info
// Display Luna's information
DisplayCatInfo(catName, catAge, favoriteToy);
#endregion
}
/// <summary>
/// Displays information about a cat.
/// </summary>
/// <param name="name">The cat's name.</param>
/// <param name="age">The cat's age.</param>
/// <param name="toy">The cat's favorite toy.</param>
static void DisplayCatInfo(string name, int age, string toy)
{
Console.WriteLine("Cat's Name: " + name);
Console.WriteLine("Cat's Age: " + age + " years old");
Console.WriteLine("Cat's Favorite Toy: " + toy);
}
}
β
Single-line comments (//
): Add short notes to explain lines of code.
β
Multi-line comments (/* ... */
): Used for longer explanations.
β
#region
and #endregion
: Group related code into collapsible sections.
β
/// <summary>
: Document methods and classes to explain their purpose.