Partial Classes in C#

后端 未结 20 2729
时光说笑
时光说笑 2021-02-19 07:59

Are there are good uses of Partial Classes outside the webforms/winforms generated code scenarios? Or is this feature basically to support that?

相关标签:
20条回答
  • 2021-02-19 08:36

    Another possible use for partial classes would be to take advantage of partial methods to make methods selectively disappear using conditional compilation - this would be great for debug-mode diagnostic code or specialized unit testing scenarios.

    You can declare a partial method kind of like an abstract method, then in the other partial class, when you type the keyword "partial" you can take advantage of the Intellisense to create the implementation of that method.

    If you surround one part with conditional build statements, then you can easily cut off the debug-only or testing code. In the example below, in DEBUG mode, the LogSomethingDebugOnly method is called, but in the release build, it's like the method doesn't exist at all - a good way to keep diagnostic code away from the production code without a bunch of branching or multiple conditional compilation blocks.

    // Main Part
    public partial class Class1
    {
        private partial void LogSomethingDebugOnly();
    
        public void SomeMethod()
        {
            LogSomethingDebugOnly();
            // do the real work
        }
    }
    
    // Debug Part - probably in a different file
    public partial class Class1
    {
    
        #if DEBUG
    
        private partial void LogSomethingDebugOnly()
        {
            // Do the logging or diagnostic work
        }
    
        #endif
    }
    
    0 讨论(0)
  • 2021-02-19 08:36

    Generally, I consider it a code smell.

    If your class is that complicated then it can probably be broken up into smaller reusable components.

    Or it means that theres no inheritance hierarchy where there should be one.

    For code generation scenarios it's good but I think code generation is another code smell.

    0 讨论(0)
  • 2021-02-19 08:38

    EDIT: DSL Tools for Visual Studio uses partial classes.

    Thus, it's a feature that many automatic generated code uses. Instead of using #region the automatic generated code goes to one file and the user code (also called custom code) goes to another and even in different directories so that the developer does not get confused with so many meaningless files.

    It's good to have this choice which you can combine - but not forced to use -with inheritance

    Also, it can be handy to separate the logic of some classes among several directories. Of course, for machines, it's the same, but it enhances the user readability experience.

    0 讨论(0)
  • 2021-02-19 08:38

    On UserControls which are relatively complicated, I put the event handling stuff in one file and the painting and properties in another file. Partial classes work great for this, Usually these parts of the class are relatively independent and it's nice to be able to edit painting and event handling side by side.

    0 讨论(0)
  • 2021-02-19 08:38

    Correction, as Matt pointed out, both sides of the partial need to be in the same assembly. my bad.

    0 讨论(0)
  • 2021-02-19 08:39

    I am late in the game... but just my 2 cents...

    One use could be to refactor an existing god class in an existing legacy code base to multiple partial classes. It could improve the discoverability of code - if proper naming convention is being followed for the file names containing the partial classes. This could also reduce the source code repository - resolve and merge to an extent.

    Ideally, a god class should be broken down into multiple small classes - each having single responsibility. Sometimes it is disruptive to perform medium to large refactorings. In such cases partial classes could provide a temporary relief.

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