0

I have see alot of old code and from a lot of developers.

#region #endregion

can be helpful some times but if is use well.

how is the best way to use it to keep my code organized and easy to read?

  • Sincerely, if my class have enough code that I must use regions to organize it, I really consider to Separate my Concerns and split it into two or more classes. Even thinking about partial classes. – gustavodidomenico Jan 23 '13 at 20:09
  • @gustavodidomenico - I couldn't agree more. – Brian Jan 23 '13 at 20:10
  • the question is very common, you're asking like "how to organize your files" [How to organize C# classes](http://stackoverflow.com/questions/3418742/how-to-organize-c-sharp-classes) or [How to organise large code files?](http://stackoverflow.com/questions/3778981/how-to-organise-large-code-files) – spajce Jan 23 '13 at 20:10
  • this is your answer: http://stackoverflow.com/questions/755465/do-you-say-no-to-c-sharp-regions – MUG4N Jan 23 '13 at 20:12
  • This is a rather non-constructive question, as it'll lead to plenty of discussion and debate on what good organisation is. However, it's a good question in and of itself, just far too general and subjective for SO and its Q&A format. =) – J. Steen Jan 23 '13 at 20:12
  • [Do you say No to C# Regions?](http://stackoverflow.com/questions/755465/do-you-say-no-to-c-sharp-regions) – spajce Jan 23 '13 at 20:16
  • welcome, next time try to construct a question that no one can ever answer because all experts here is strict :D – spajce Jan 23 '13 at 20:25

2 Answers2

4

It doesn't mean anything special if that's what you're asking. The only special effect it has is within Visual Studio for code folding.

If you have a large class that performs a few tasks, it may be best to region it out by separating your properties, different groups of methods, interface implementations, and whatever else you think may be important. There are no strict rules.

class MyReallyBigClass : IAwesome, INotAsAwesome
{
    #region Public Properties
    public string Test { get; set; }
    // ..
    #endregion

    #region IAwesome Implementation
    public void IAwesome.BeAwesome()
    {
        // ..
    }
    public int IAwesome.AwesomeLevel()
    {
        // ..
    }
    #endregion

    #region INotAsAwesome Implementation [[...]]

    #region Internal Fields
    private int _whatever;
    // ..
    #endregion
}

Of course in practice, you wouldn't really get a class so large that you'd need to separate it out, but I normally do find myself using it around properties and interface implementations at the least.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
  • Just stumbled upon this question for no reason. Sorry but "you wouldn't really get a class so large that you'd need to separate it out" is a dream. I got to work on 15K LoC classes, and ones that have even more (those we actually split into several files). Therefore, in case any of my classes should in the future become one of those, I'll split it from the beginning. – Squirrelkiller Nov 23 '18 at 07:44
  • @Squirrelkiller This was specific to regions, a 15K LoC class should never have come to be and is normally a result of years of cargo culting. – Rudi Visser Nov 23 '18 at 10:03
2

It's purely to aid readability in the IDE. It's effectively stripped out on compilation.

That said, I tend to defer to Microsoft's style guidelines with regards to usage: grouping methods, properties, constructors, etc. - rarely, if ever, inside a method, and never between a brace construct (if, for, etc) and the opening brace.

JerKimball
  • 16,584
  • 3
  • 43
  • 55