Outlining in Visual Studios – What Is It and How Do We Use Them?

What are the five outlining options and why are they important?

Microsoft Visual Studio offers an endless supply of tools that make life easier for developers. A great tool that has been included in Visual Studio is the Outline tool. To find outlining in your toolbar, click Edit > Outlining or use its respective shortcuts. This tool allows for clean, legible code as your files become more complex. Outlining creates a clearer vision of the code you wish to see by minimizing other code. Visual Studio has created multiple options for alternative approaches, including Toggle Outlining Expansion, Toggle All Outlining, Stop Outlining, Stop Hiding Current, and Collapse to Definition. Let’s quickly run through the options and when you will need them in the following paragraphs.

1. Toggle Outlining Expansion

The Toggle Outlining Expansion option allows you to quickly collapse or expand the current code block. This is different from Ctrl+M, Ctrl+M, which collapses or expands the code block that your cursor is currently inside. The Toggle Outlining Expansion command works based on the outlining icons in the margin of the editor window.

To use Toggle Outlining Expansion, click the outlining icon for the current code block in the margin of the editor window. If the code block is currently expanded, clicking the icon will collapse it, and if it is currently collapsed, clicking the icon will expand it.

For example, suppose you have the following code:

void MyMethod()
{
    if (condition1)
    {
        if (condition2)
        {
            // code goes here
        }
    }
}

If your cursor is inside the inner if statement, pressing Ctrl+M, Ctrl+M will only collapse that statement. However, clicking the outlining icon for the outer if statement in the margin and selecting Toggle Outlining Expansion will collapse both the outer and inner if statements:

void MyMethod()
{
    if (condition1) { ... }
}

2. Toggle All Outlining

The Toggle All Outlining option allows you to quickly collapse or expand all outlining in the current document. This can be useful if you want to quickly hide all code and focus on the high-level structure of the document.

To use Toggle All Outlining, select Edit > Outlining > Toggle All Outlining from the menu bar or press Ctrl+M, Ctrl+L. This will toggle the collapsed or expanded state of all code blocks in the document.

For example, suppose you have a long file with many methods and if statements. Selecting Toggle All Outlining will collapse all of them, leaving only their outlining icons visible:

void MyMethod1() { ... } // outlining icon
void MyMethod2() { ... } // outlining icon
if (condition) { ... } // outlining icon

3. Stop Outlining

The Stop Outlining option allows you to turn off outlining for the current document. This means that all outlining icons and guide lines will be hidden, and you will not be able to collapse or expand code blocks.

To use Stop Outlining, select Edit > Outlining > Stop Outlining from the menu bar. This will turn off outlining for the current document.

4. Stop Hiding Current

The Stop Hiding Current option allows you to unhide the current code block if it is currently collapsed. This is useful if you want to temporarily expand a code block to see its contents.

To use Stop Hiding Current, place your cursor inside the collapsed code block and select Edit > Outlining > Stop Hiding Current from the menu bar. This will expand the code block and reveal its contents.

For example, suppose you have the following code:

void MyMethod()
{
    // code goes here
}

If you collapse the method by clicking its outlining icon in the margin of the editor window, it will look like this:

void MyMethod() { ... } // outlining icon

To temporarily expand the method to see its contents, place your cursor inside the collapsed code block and select Stop Hiding Current.

5. Collapse to Definition

The Collapse to Definition option allows you to quickly collapse a code block to its definition. This means that all code inside the block will be hidden, and only the block’s signature or header will be visible. This can be useful for quickly navigating large files or for hiding blocks of code that you’re not currently working on.

To use Collapse to Definition, simply right-click on the code block’s outlining icon in the margin of the editor window and select Collapse to Definition. Alternatively, you can press Ctrl+M, Ctrl+O while the cursor is on the code block’s signature.

Here’s an example of how Collapse to Definition works. Let’s say you have a method with the following code:

public void MyMethod()
{
    // Some code here
    if (someCondition)
    {
        // Some more code here
    }
}

If you want to collapse the if statement to just its signature, you can right-click on the outlining icon for the if statement and select Collapse to Definition. The resulting code will look like this:

public void MyMethod()
{
    // Some code here
    if (someCondition) { ... } // outlining icon
}

Now, you can expand and collapse the if statement by clicking on the outlining icon, without seeing the details of the code inside. This can be a helpful way to focus on the high-level structure of your code without getting bogged down in the details.

Conclusion

Outlining is an easy, yet powerful tool to use as you increase your skill as a programmer. It helps to display this code in a digestible way to your team, managers, clients, or anybody interested in your code. Luckily, Visual Studio has made it very simple for developers to grasp outlining and only takes seconds to implement. If you want to take your coding demonstrations, we suggest you learn Outlining!

Our team here at LearningDot.NET hope you enjoyed this tutorial on how and why to use Outlining in Visual Studio. If you had any questions, please leave a comment below and one of our experts will be happy to respond. If you found this tutorial helpful, please leave a comment on your experience, we love interacting with our readers. Happy Programming!

Leave a Reply

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