C# – #region/#endregion vs sub functions

cvisual studio

How do you recommend using #region / #endregion? To what extent should that replace using sub functions to clarify your code?

Best Answer

Not at all.

First of all, #regions are more a way of grouping many related functions/members into collapsible regions. They are not intended to structure a single multi-thousand line function into parts. (That being said, if you write a single method that's so long that you consider structuring it with #regions then you're probably doing something seriously wrong. Regions or not, that code would be unmaintainable. Period.)

Many people argue however, that it doesn't really help and that you should consider rewriting classes that actually need regions to be understandable. Also, regions tend to hide nasty code.