How do I add comments in the source code?

A well written program usually become a document for itself. Using a good and descriptive fields or method names for example helps the reader of the code to understand the code better.

To intentionally adds comments to our program we can use a single-line comment or a multi-line comments. A single line comment starts with a double forward slash (//) and continues to the end of the line. The multi-line comment starts with /* and ends with */ symbol and it can span in multiple lines.

There is also a documentation comment that contains some XML-formatted data that can be use to create a documentation for classes or methods. This type of documentation can be extracted from the source code to create a documentation file. An example is shown in the sayHello(string name) method in the snippet below.

using System;

namespace Kodecsharp.Example.Intro
{
    class CommentExample
    {
        [STAThread]
        public static void Main()
        {
            // Assign Alice to name.
            String name = "Alice"; // This is a single line comment.
            Console.WriteLine("Name: " + name);

            /*
             * A multiline comments.
             * Creates an array of integer numbers and prints its
             * value using the foreach statement.
             */
            int[] numbers = { 1, 1, 2, 3, 5, 8, 13, 21 };
            foreach (int number in numbers)
            {
                Console.WriteLine("Number: " + number);
            }

            // Call the sayHello() method.
            sayHello("Jane");
            sayHello(null);
            Console.ReadLine();
        }

        /// <summary>
        /// This method prints a hello message to the user.
        /// </summary>
        /// <param name="name">The name of the user</param>
        public static void sayHello(string name)
        {
            if (name != null && !name.Trim().Equals(""))
            {
                Console.WriteLine("Hi, Hello " + name + "!");
            }
            else
            {
                Console.WriteLine("Hi, Hello There!");
            }
        }
    }
}