Whether you're a beginner or an experienced developer, knowing how to comment out multiple lines of code in Visual Studio can save you time and help you debug, test, or document your code more efficiently. Fortunately, Visual Studio offers several ways to do this—whether you prefer keyboard shortcuts, toolbar buttons, or manual comment markers.
In this guide, we’ll walk you through all the methods available to comment out multiple lines in Visual Studio, regardless of the programming language you’re using (C#, C++, HTML, etc.).
Why Comment Out Code?
Commenting out code is useful for many reasons:
- Temporarily disable parts of your code without deleting them.
- Add explanations to help you or other developers understand the logic.
- Debug your code by isolating problematic sections.
- Test different logic without removing the original code.
Visual Studio provides powerful tools to make commenting faster and easier, especially when working with multiple lines.
Method 1: Use Keyboard Shortcuts
The easiest and fastest way to comment or uncomment multiple lines in Visual Studio is by using keyboard shortcuts.
For C#, C++, JavaScript, TypeScript, and similar languages:
Comment multiple lines:
Select the lines you want to comment and press:
Ctrl + K, then Ctrl + C
Uncomment multiple lines:
Select the lines and press:
Ctrl + K, then Ctrl + U
This method works in most common code files and is a favorite among developers due to its speed and simplicity.
Method 2: Use the Toolbar Buttons
If you prefer using the mouse or are not comfortable with keyboard shortcuts, you can use the toolbar buttons in Visual Studio.
Select the lines of code you want to comment.
In the toolbar, find the buttons labeled:
Comment Out the Selected Lines (Ctrl+K, Ctrl+C)
Uncomment the Selected Lines (Ctrl+K, Ctrl+U)
- Click the appropriate button.
💡 If you don’t see these buttons, you may need to customize your toolbar:
Right-click the toolbar and choose Customize.
Add the Text Editor commands to make the comment options visible.
Method 3: Manually Add Comment Symbols
If you’re working in a language where shortcuts aren’t available or you’re in a basic editor mode, you can manually add comment symbols.
Example in C#, C++, or JavaScript:
// This is a single-line comment
// This is another line
/* This is a
multi-line comment block */
Example in Python:
This is a single-line comment
This is another comment line
🔁 In Python and some scripting languages, there is no multi-line comment block. You’ll need to add # at the start of each line or use a string block (""" ... """) in some cases.
Bonus: Commenting in Razor or Mixed Code Files
If you’re working in Razor (.cshtml) or similar files that mix HTML and C#, be aware that commenting works differently depending on the code section. Use @* *@ for Razor comments, and for HTML.
Example:
@* This is Razor comment *@
Final Thoughts
Knowing how to quickly comment and uncomment multiple lines in Visual Studio is a simple yet powerful skill that can drastically improve your productivity. Whether you’re testing code, collaborating on a project, or simply organizing your work, using the keyboard shortcuts (Ctrl+K, Ctrl+C and Ctrl+K, Ctrl+U) or toolbar buttons makes the process fast and efficient.
So next time you're experimenting or troubleshooting your code, don't waste time deleting and rewriting—just comment it out!