Is the #region directive really useful in .NET?

后端 未结 17 1906
孤城傲影
孤城傲影 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:15

    A similar question has already been asked.

    but...

    I would say not anymore. It was originally intended to hide generated code from WinForms in early versions of .NET. With partial classes the need seems to go away. IMHO it gets way overused now as an organizational construct and has no compiler value whatsoever. It's all for the IDE.

    0 讨论(0)
  • 2021-01-04 00:15

    I hate the over-use of these. The only think I find them useful for is hiding away things you probably never want to see again. Then again, those things should probably be off in a library somewhere.

    0 讨论(0)
  • 2021-01-04 00:20

    I don't generally use code regions, except in one specific case - dependency properties. Although dependecy properties are a pleasure to work with in most respects, their declaraions are an eyesore and they quickly clutter your code. (As if managing GUI code was not already enough of a challenge...)

    I like to give the region the same exact name as the CLR property declaration (copy/paste it in there). That way you can see the scope, type and name when it's collapsed - which is really all you care about 95% of the time.

       #region public int ObjectDepthThreshold
    
        public int ObjectDepthThreshold
        {
            get { return (int)GetValue(ObjectDepthThresholdProperty); }
            set { SetValue(ObjectDepthThresholdProperty, value); }
        }
    
        public static readonly DependencyProperty ObjectDepthThresholdProperty = DependencyProperty.Register(
            "ObjectDepthThreshold",
            typeof(int),
            typeof(GotoXControls),
            new FrameworkPropertyMetadata((int)GotoXServiceState.OBJECT_DEPTH_THRESHOLD_DEFAULT,
                FrameworkPropertyMetadataOptions.AffectsRender,
                new PropertyChangedCallback(OnControlValueChanged)
            )
        );
    
        #endregion
    

    When it's collapsed you just see

    public int ObjectDepthThreshold
    

    If I have more than one dependency property, I like to start the next #region on the very next line. That way you end up with all of them grouped together in your class, and the code is compact and readable.

    BTW if you just want to peek at the declaration, mouse hover over it.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-04 00:24

    http://www.rauchy.net/regionerate/ - Automatically regionised your code ;)

    I'm a fan of regions for grouping sections of large classes, say all the properties together, all constances, etc. I'm someone who's constantly collapsing code I don't need to see at that time so I love regions for that.

    Also I find regions really useful when implementing interfaces, particularly multiple interfaces. I can group each interfaces methods, properties, events, etc so it's easier at a glance to see what method belongs to what interface.

    0 讨论(0)
  • 2021-01-04 00:24

    I love regions because it helps me focus on just what I am working on. I use them even if the class just has a method.

    I use code snippets with regions already pre-populated, which is less typing. I feel the class is more organized and does what Code Complete talks about make it nicer for other people to read. The compiler just ignores them, they are now to make code more readable.

    0 讨论(0)
提交回复
热议问题