Friday, May 13, 2005

Do you #region?

In C#, how often do you use #region? Lately, I find myself using them only because "I should".

For the most part, I think #region indicates the need for refactoring. Other times, I use #region to hide all my private fields or something similar. Usually, there are so few of these per class that it's unneeded (but it looks nice?). In the end, it's probably more trouble opening and closing them when I need to add new fields.

There are times that a #region makes sense. If you have repeating code, that cannot be refactored, a #region with a short description can be quite nice. See Accessing Private Members in C# for an example. This is rare though, always look for refactoring before settling on a #region. Also, in .NET 1.1 #region is great around generated code. However, in .NET 2.0, with the introduction of partial classes there seems to be less and less need for #region.


  1. If you are using #region to group a part of code, it is probably too big.

    Also if you want to do that, it probably needs to be a method or seperate class by itself.

    If you cant refactor the code, it doesnt mean no one else can. Maybe someone in the project has some bright sparks.

    If you hide the code in the region it defeats the purpose of encouraging others to reafactor your code.

  2. Anonymous5:50 AM

    So, if this is your first time, or you are a returning visitor, please sign my guest
    book and tell me about yourself at auto loans


Note: Only a member of this blog may post a comment.