C# Tutorial – Comments in code


Facebooktwitterlinkedinmail

In C# you can have 3 types of comments. Each have different syntax and a bit different usage.

One line comment

The simplest type of comment is one line comment. It’s started by //  and last until end of line. You can find and example in recent post (C# Variables and build-in types) or in example below (taken from C# Hello World appliaction).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // This comment will last until end of line
            Console.WriteLine("Hello, World!");
        }
    }
}

 

Multi-line comment

For more complex comment you can use multi-line comments. As those span many lines they need not only clear indication of start but also the end marker. To start multi-line comment in C# you have to use /*  and the end should be marked by */ . Example below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            /* This comment can span
            many lines */
            Console.WriteLine( /* Can be placed inside statements as well */ "Hello, World!");
        }
    }
}

 

XML documentation comment

This is very special type of comment. It can make developer life easier in some cases. You can use this comment to document your code (methods, classes, etc). This is highly recommended for public APIs.

Let’s say you want to document your Main method. You need to insert new line before the method and type /// .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFirstApplication
{
    class Program
    {
        ///
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Then Visual Studio will auto complete it to the proper shape.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFirstApplication
{
    class Program
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Between summary markers you can put description of the method. Inside param markers you can put parameter description. Let me give you an example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFirstApplication
{
    class Program
    {
        /// <summary>
        /// Main entry point of the application
        /// </summary>
        /// <param name="args">Application arguments</param>
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Ok, it’s done, but why is it useful? Let’s try it. Add new line of code and start typing Program. Note that hint will pop-up after you pressed the .Main

image

Yes, there is your summary. Let’s now try to execute the method – add (  char. You will see the hint for the args parameter.

image

It is a good practice to write self-explanatory code that don’t need additional comments. But sometimes it’s just not possible and in such cases you should add documentation comments that will help anyone that need to use it.

 

Visual Studio shortcuts

EDIT: There are two useful shortcuts that you can use to comment / uncomment code.

  • Ctrl+K, Ctrl+C – comment code
  • Ctrl+K, Ctrl+U – uncomment code

If you are in editor and have no code selected above commands will comment / uncomment current line. If you have some code selected the commands will comment / uncomment all lines with selected code – full lines no matter how much text you will select. Each line will be commented using one line comment (// ).

Let me show an example – first I select following code:

Select code first

Then after pressing Ctrl+K, Ctrl+C:

Comment code

I’ll do a separate post about the most useful shortcuts.

Facebooktwitterlinkedinmail

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 thoughts on “C# Tutorial – Comments in code