Q. #region blocks are being put into my code by the VS.NET wizards, but I have no idea what they're for. Hold me, Chris. I'm scared.

Asked by reader. Answered by the Wonk on October 17, 2002

A.

Don’t panic. The VS.NET code editor window knows how to collapse and expand certain regions of your code with little plus and minus signs in the margins. This feature is called outlining and allows you to, for example, collapse the entire implementation of one class while you’re concentrating on another one. However, only certain constructs can be outlined, like classes, namespaces and methods. For other things, like a group of event handler methods, you can define a custom group for outlining using a region, defined using the #region/#endregion directives. For example, the Implement Interface feature of VS.Net produces a region for the method implementations labeled “<InterfaceName> Methods”. Collapsed, it looks like this:

There are a number of operations that can be performed on a region of outlined code, as shown in the Edit->Outlining menu:

Toggle Outline Expansion: Expand or collapse the current region

Toggle All Outline: Expand or collapse the current file

Stop Outlining: Turn off the outlining feature for all text files

Collapse To Definitions: Hide all code and show just namespace, class and member definitions