After maintaining lots of code littered with #region (in both C# and VB.NET), it seems to me that this construct is just a bunch of \"make work\" for the programmer. It\'s w
Our Business Objects all have Regions - and we love them.
We have;
We have a few others depending on the type of Business Object we are dealing with (Subscriber etc)
For many classes regions just get in the way - but for our standard business objects they save us a ton of time. These Business Objects are code gen'd, so they are very consistent. Can get to where I want to be way faster than the clutter if they aren't, and the consistency makes it easy to find each other's stuff.
They can be overused, but I like them for separating private methods, public methods, properties, and instance variables.
Going on with what has been previously said by Russell Myers, if you learn how to refactor your code properly (a skill proficient developers must learn), there really isn't too much of a need for regions.
A couple of weeks ago I thought regions were great because they allowed me to hide my fat code, but after exercising my code skills I was able to make it slimmer and now I fit into a size 7 class (someone should SO make that a measurement for refactoring in the future! :P)
There are times when your methods HAVE to be long, especially with web development. In those cases (such as when I've got a gridview with a large, complex object bound to it) I've found it useful to use regions:
#region Declaring variables for fields and object properties
#region Getting the fields in scope
#region Getting the properties of the object
#region Setting Fields
These are discrete sections of the method that COULD be broken out, but it would be difficult (I'd have to use variables with larger scope than I like or pass a LOT of variables as 'out'), and it is basic plumbing.
In this case, regions are perfectly acceptable. In others, they are needless.
I will also use regions to group methods into logical groups. I reject partial classes for this purpose, as I tend to have a lot of pages open when I'm debugging, and the fewer partial classes there are in an object (or page, or dialog), the more of them I can have on my tab list (which I limit to one line so I can see more code).
Regions are only a problem when used as a crutch, or when they cover poor code (for instance, if you are nesting regions inside of each other within the same scope, it's a bad sign).
I use them all the time. Again, like anything else, they can be used for both evil and good, and can certainly be the hallmark of bad design, but they can be used to help organize code very well.
#region Properties
#region Update Section
#region Accessors
Certainly you should avoid Jeff's example of
#Sweep under carpet
What I find odd about them, as Jeff pointed out, is that they are a compiler preprocessor command for ui purposes. I'm sure VS team could have done something just as useful in another way.