Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices: When not/to use partial classes

I have been using the partial class modifier for some time in order to put helper classes in their own file.

Today we got a new guy and he said that the last team he worked with didn't allow partial classes for this because modifying a helper class that is in a separate file would cause the main partial class file to get out of whack with the changes. Also, they were only allowed to put a helper classes inside of the main class as the last resort so that everything remained decoupled.

What do you think? Is there any problem using partial classes like this or does it boil down to preference?

For instance, I usually have something like this:

  • MainClass.cs
  • MainClass.Helper1.cs
  • MainClass.Helper2.cs

...

// Inside of MainClass.cs I have code like this:  public abstract partial class MainClass {     // ... }  // Then in the MainClass.Helper1.cs I have:  partial class MainClass {    private class Helper1    {        // ...    } } 
like image 904
Wayne Bloss Avatar asked Dec 08 '08 23:12

Wayne Bloss


People also ask

When should you use partial classes?

Use of Partial ClassesIf you are working on a bigger project, splitting the files over different classes helps developers work on the same project simultaneously. If you are working on an automatically generated source, then the code can be added to the class without regenerating the source file.

What are all the rules to follow when working with partial class definitions?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

What will happen if there is an implementation of partial method without defining partial?

Similar to a partial class, a partial method can be used as a definition in one part while another part can be the implementation. If there is no implementation of the partial method then the method and its calls are removed at compile time. Compiler compiles all parts of a partial method to a single method.


2 Answers

Partial classes are primarily for code-generator usage, such as designers - but I use the approach you have cited - in particular when an object implements multiple (non-trivial) interfaces, I find it useful to break it up 1 file per interface implementation. I also commonly have a file for the static methods, which are usually different enough from instance methods to warrant separation.

like image 89
Marc Gravell Avatar answered Oct 02 '22 16:10

Marc Gravell


Personally I can't see anything wrong with using partial classes like this, but that's just my own opinion. The only thing that might seem like "bad practice" is to name your classes "Helper1" and "Helper2" (but that might be an example only for clarification).

If you're using partial classes like this, check out the (free) addin vsCommands (for Visual Studio 2008) that makes it really easy to group files in the solution explorer (just like designer files) without editing the project file.

like image 21
Patrik Svensson Avatar answered Oct 02 '22 16:10

Patrik Svensson