What are the advantages and disadvantages of using #regions
in C#
?
Does your use really make it easier to organize your code?
#region
/* Código */
#endregion
What are the advantages and disadvantages of using #regions
in C#
?
Does your use really make it easier to organize your code?
#region
/* Código */
#endregion
It's irrefutable that regions make organizing easier, but care should be taken with what you really plan to organize.
I recommend using them when you have the following purposes in mind:
I believe that the use of regions to separate members of a class according to protection levels (private, public, etc) is justifiable, but I define their use as preferential since, the best thing to do is to group them according to their functionality, in others it becomes more feasible to organize them based on the needs of your application.
Now, using regions to hide "ugly" code is reprehensible; when the intention is to hide, why not avoid future confusions and spend a small part of the time in refactoring certain functionality?
The idea is to decrease the amount of code displayed on the screen by grouping common functions. The advantage is to leave the code more succinct for reading.
The disadvantage is that the regions hide the signatures of the methods. When it is important to see the signatures of each method, the shortcut Ctrl + M , Ctrl + O .
This is just a way to organize code, and is usually used by generated code.
In non-generated codes I recommend a certain care when using regions, because when used to separate codes according to any classification, they tend to get outdated.
The use I recommend is to group members who have some real connection. For example: