Visual Studio Comment Selection

Visual Studio logo (Copyright Microsoft)

You can use Visual Studio to automatically comment out one or more lines of code in your C# program. This is done with the Comment Selection option. You can then use Uncomment Selection to remove the comment characters.

This Visual Studio article covers the Comment Selection and Uncomment Selection features.

Examples

Let's begin by looking a simple C# program. It's not the most useful program ever written, but it serves its purpose here. Try selecting the two lines in the Main method and right-clicking your mouse. Click on "Comment Selection" in the context menu. You will see the same code as is shown in the second example.

First example [C#]

class Program
{
    static void Main()
    {
	int value = 1;
	value *= 2;
    }
}

Second example [C#]

class Program
{
    static void Main()
    {
	//int value = 1;
	//value *= 2;
    }
}

Uncomment Selection. Next, we can try the Uncomment Selection context menu item. This command will remove a // sequence from the start of every line that has one. If no // sequences are found, it does nothing.

Details

Note (please read)

You can use Comment Selection on the same text multiple times; multiple // sequences are added. Uncomment Selection only removes a single // sequence each time it is called. This information is current for Visual Studio 2010.

Summary

Comment Selection and Uncomment Selection are useful options in Visual Studio, but only if you know they are there. I have to admit I didn't discover them after coding in Visual Studio for over four years. I prefer my // comments to have a space following them, but this is not important to the compiler or to many other programmers.

Visual Studio Tips
.NET