C# Comments

HTML
CSS
C#
SQL

Comments

Comments are your programming storytellers. They don’t affect how your code runs but provide essential insights for you and others reading your code.

// This is a single-line comment

/*

   This is a multi-line comment

*/

 

// Let’s use comments to explain our code from the previous examples:

 

using System;

 

class ExampleWithComments

{

    static void Main()

    {

        // Loop with a break statement

        for (int i = 1; i <= 10; i++)

        {

            if (i == 5)

            {

                Console.WriteLine(“Found the magic number!”);

                break; // Exit the loop when i is 5

            }

            Console.WriteLine($”Current number: {i}”);

        }

 

        // Loop with a continue statement

        for (int i = 1; i <= 5; i++)

        {

            if (i == 3)

            {

                Console.WriteLine(“Skipping the third step!”);

                continue; // Skip the rest of the loop body and continue with the next iteration

            }

            Console.WriteLine($”Current step: {i}”);

        }

 

        // Loop with a goto statement

        int counter = 0;

    start:

        Console.WriteLine($”Current count: {counter}”);

 

        if (counter < 5)

        {

            counter++;

            goto start; // Jump back to the ‘start’ label

        }

    }

}

Course Video