Visual Studio Tips and Tricks - X

(This is a part of my series Visual Studio Tips and Tricks – IV )

There at times when we write long nested codes in the same file. To organize our codes we split the logic in different classes and files, and yet we still need to organize it further down. With the introduction of C# region directives, it helped us organize pieces of statements to form a collapsible region as show below.

This is a pretty good solution to organize the codes. But as we keep adding them, it soon turns out to be more messy than ever before. Sometimes we might even prefer to temporarily collapse a particular block. This is not a problem for classes or methods. But what if we want the same for any arbitrary statements or piece of code? Wouldn’t that be great?

No worries. VS already has a solution for us. Its always been there in front of us and yet we failed to discover.

stop it … and show me how?

Ok. First of all select the the code you want to collapse then right click the editor and click Outlining > Hide Selection.

Did you notice something weird? I actually selected only the condition inside the if statement and hid the selection. It would then look as below.

Isn’t that cool?

Now try closing that file and reopen. VS is smart enough to remember the old collapsible selection.

So, the shortcut of the day would be Ctrl + M, Ctrl + H to hide the current selection.

If you would like to see more Visual Studio tips and tricks page. Please visit Visual Studio Tips and Tricks.