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


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:



I have feedback on this Ask The Wonk answer