
Master Regions #9
Chapter 2, Master the Editor
|
47
HACK
In C#, you can include regions anywhere in your source code file, even
inside of methods or properties. (Whether this is a good idea is debatable.)
Regions can also be nested inside of other regions.
VB.NET Regions
VB.NET regions are a little bit different than C# regions. In VB.NET, you
surround the code that you want to include in the region with
#Region and
#End Region. You can also include a string after the #Region, but in VB.NET,
the string will need to be enclosed in quotes. Figure 2-17 shows an example
of VB.NET code surrounded by a region.
You might notice that in VB.NET, region tags are always moved to the com-
plete left of the document. In VB.NET, you can include regions inside of
other regions, but you can’t include them inside of methods or properties.
Region Strategy
Regions are best used to group similar parts of your class. This makes find-
ing a particular method or property easier and also makes the file more read-
able, since sections that you are not working with are hidden inside of a
region. I am a big fan of the idea that all code, except for namespace declara-
tions and using statements, should be in a region. Opening a file in which all
of the private member variables are grouped inside of a region, the construc-
tors in another region, the private methods in another region, and the pub-
lic properties in yet another region ...