Is the #region directive really useful in .NET?

后端 未结 17 1907
孤城傲影
孤城傲影 2021-01-03 23:43

After maintaining lots of code littered with #region (in both C# and VB.NET), it seems to me that this construct is just a bunch of \"make work\" for the programmer. It\'s w

17条回答
  •  隐瞒了意图╮
    2021-01-04 00:22

    Like any language feature, regions have the potential to be misused and abused but they also have their benefits.

    They are great for creating "folding" groups around:

    • methods, especially if you have a lot of overloaded methods
    • interface implementations
    • operator overloads

    You can also use it to group properties, public/private methods, events, class-wide variables, etc.

    I use regions in my code to help create a consistent structure in my code so I always know where things are at a glance. Yes, it makes things a bit harder during refactoring or adding new functions (especially when autogenerated by Visual Studio) but I feel it's a small price to pay to keep things consistent and structured.

提交回复
热议问题