I suggest you ...

#region Directive Considered Harmful (was "Get rid of #region")

My informal analysis concludes that #regions are used for evil 96% of the time.

430 votes
Sign in
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
K. Scott Allen shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Hey Scott,

Thanks for taking the time to make this suggestion. I think every programmer has one or more features in any language which they just hate because they’ve been abused by someone else. I’ve renamed this post to capture that immortalized hatred all programmers share for such evil constructs!

Having said that, #regions do have valid use 4% of the time to someone (arguably grouping interface implementations within a class?) and even if there weren’t it’s really far too late to remove them (even if we thought they were evil, which we don’t). It simply wouldn’t be responsible to the millions of developers out there who might have #region directives in their code today either intentionally or having inherited them from other developers (at least some of which have passionately argued on this thread below) to break that code now without a pretty darn good reason. As a rule, generally speaking it’s somewhere the C# language design team just doesn’t go.

Luckily we have tools like StyleCop and one day “Roslyn” which can be leveraged to enforce our dreams of code purity, at least within our own spheres of influence.


-Anthony D. Green, Program Manager, Visual Basic & C# Languages Team


Sign in
Password icon
Signed in as (Sign out)
  • Michel Pavan Macedo commented  ·   ·  Flag as inappropriate

    Finally someone with a great solution (@dandraka), but I don't get the X number of lines thing...

    I guess a warning plus rules for gendarme, fxcop and stylecop would do the trick.

  • Harri Koppel commented  ·   ·  Flag as inappropriate

    Totally agree. People keep putting them back after all the trouble I've been through deleting them.

  • dandraka commented  ·   ·  Flag as inappropriate

    As other have mentioned, #regions are a code smell. I can't imagine MS removing a feature that so many users are using though !
    I would suggest that any #region with more than X number of lines could generate a #warning (other suggestions welcome).

  • Mario commented  ·   ·  Flag as inappropriate

    @matt65: Hiding is just a feature of #region, much like you can collapse a method or a property, But mostly I think it's about organizing your code.

  • matt65 commented  ·   ·  Flag as inappropriate

    If you feel the need to hide parts of your class, then you need to simplify your class.

  • Philippe commented  ·   ·  Flag as inappropriate

    #region are good to hide private stuff. In fact, what would be nice would be that the IDE would be able to add event hadler in a region (either with a specific name or in an existing region that already has event handlers (last one).

  • EricTN commented  ·   ·  Flag as inappropriate

    @Mads Kristensen That's terrific news, we're really looking forward to it! :)

  • Cesar commented  ·   ·  Flag as inappropriate

    It should be kept.

    I don't know how many evil people you met lately, but definetly they are not around me firing their #regions on my code.


  • Anonymous commented  ·   ·  Flag as inappropriate

    I try not to be evil nevertheless I make extensive use of #region, beleiving that it clarifies my code, please do not remove it.

  • Shiv Kumar commented  ·   ·  Flag as inappropriate

    Scott, don't use them if you don't want. Just don't dictate how others should use their tools.

  • Morten Nielsen commented  ·   ·  Flag as inappropriate

    I don't get how this is "evil". If you don't like it and it doesn't fit your coding style, well don't use it, but don't ask to remove a feature that other people use and love, just because YOU feel it's wrong (not to mention removing it would be a breaking change, which alone would mean this feature request would never go through).

  • Mario commented  ·   ·  Flag as inappropriate

    @JarretV: I don't think people "need" to use regions, but those who do use them, probably just wants to. I can't really see how SRP applies to the use of regions.

    Building a module with a single responsibility does not mean that you cannot use regions. I mean, the module can still have properties, fields, public/private methods etc. that someone would like to use regions to organize the appearance of the code and still have a single responsibility.

  • EricTN commented  ·   ·  Flag as inappropriate

    Our shop wraps regions around properties, methods, the constructor and fields areas of a class. We love the fact that when you open a file these standard areas are well-identified and facilitate drilling-in to the part of a class you need to adjust. Rather than no more regions, we would love to see regions implemented where they currently aren't: javascript files, html files, css files, etc.

  • matthewdeanmartin commented  ·   ·  Flag as inappropriate

    There are lots of ways they could implement this-- make them open by default and non-collapsable by default, a button to globally replace them, a compiler warning or better yet a compiler error.

    The worst crime of #region is that is is there and collapses by default instead of the tiny percent of people that like #regions having to enable it.

Feedback and Knowledge Base