What are the Basics of Comments in C#
Tags: csharp, dotnet, tutorial, beginners
Published: 2023-11-16 04:19:06

{% youtube https://www.youtube.com/watch?v=A4HBSZXOyxE %}
Watch the [full video](https://www.youtube.com/watch?v=4X1x3yJwbyM&t=7s) on YouTube.

## 1️⃣ Single-line Comment

It is easy to write a Single-line comment.👩‍💻

Just type two fore-slash or forward slashes(/). And you can write whatever you want. But, the lines above and below are unaffected.

```csharp
// Single-line Comment
Console.WriteLine("Above");
// Whatever you want.
Console.WriteLine("Below");
```

You can also add it beside the code, or on top to add a short description.

```csharp
// Calculates the next leap year.
var nextLeapYear = DateTime.Now.AddYears(4); // Description here.
```

There’s no rule to add space between fore-slash and the comment, but this is more readable:

```csharp
// Calculates the next leap year.
```

than this:
```csharp
//Calculates the next leap year.
```

## 2️⃣ Multi-line Comment

If a Single-line comment isn’t enough:

```csharp
// TODO: 1. Optimize this code. 2. Google or go to StackOverflow. 3. Copy-paste the code. 4. Refactor again.
```

Try Multi-line comments. Type fore-slash then asterisk(*), and press Enter. You can end this by typing a closing fore-slash. 

```csharp
/*
 * Multi
 * Line
 * Comment
 */
```

Pretty useful for TODO lists like for the example earlier.

```csharp
/*
 * TODO: 
 * 1. Optimize this code. 
 * 2. Google or go to StackOverflow. 
 * 3. Copy-paste the code. 
 * 4. Refactor again.
 */
```

## 🧱 Blocks of code: Commenting and Uncommenting

Press **CTRL + K + C** to comment your codes. It’s really handy when you have a huge chunk of code like this one:

```csharp
//public double GetFruitPrice(string fruitName)
//{
//    const int FRUIT_NAME_NOT_FOUND = -1;
//    switch (fruitName)
//    {
//        case "Apple":
//            return 5.99;
//        case "Banana":
//            return 3.99;
//        case "Grapes":
//            return 7.99;
//    }
//    return FRUIT_NAME_NOT_FOUND;
//}
```

Press **CTRL + K + U** to uncomment:

```csharp
public double GetFruitPrice(string fruitName)
{
    const int FRUIT_NAME_NOT_FOUND = -1;
    switch (fruitName)
    {
        case "Apple":
            return 5.99;
        case "Banana":
            return 3.99;
        case "Grapes":
            return 7.99;
    }
    return FRUIT_NAME_NOT_FOUND;
}
```

Just remember to use comments sparingly because it can produce dead codes. 

Here's the final code you can [view on GitHub Gist](https://gist.github.com/pangan-dev/75aa70ab0d7864c9b2dff2cd5ad4bdbd).
{% embed https://gist.github.com/pangan-dev/75aa70ab0d7864c9b2dff2cd5ad4bdbd.js %}

And here's the code for commenting and uncommenting that's also [available on GitHub Gist](https://gist.github.com/pangan-dev/5d6217e2429d4c16b5012f84471632e1){% embed https://gist.github.com/pangan-dev/5d6217e2429d4c16b5012f84471632e1.js %}

Thanks for reading! 📖

Check out my [YouTube channel](https://www.youtube.com/@Emmanuel-Pangan) for more coding tutorials. Happy coding everyone! 🧡

Source(s):
[Microsoft's documentation about comments](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/comments)