0

Partial classes feel like inheritance to me. Sometimes I find myself wondering weather I should derive or partialize or make an extension. All three extend the behavior of a type.

Some things that helps me decide between equivalent mechanisms are:

  • What element of OO programming does it leverage?

  • Is it considered a first choice or a fall back?

Am I correct in viewing partial classes like inheritance and should their use be limited to certain special cases?

A bonus would be if someone could give me some historical background - who thought them up? was their a specfic problem that they originally solved, comparable to how extension methods facilitated LINQ?

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121

5 Answers5

4

Partial types aren't inheritance, plain and simple.

Partial types are simply a way of separating the code for a single type into multiple source files - typically so that some can be designer-generated and others not. Nothing more or less than that.

The specific problem they solved was that separation between designer code and manual code - avoiding the "do not touch this bit of the file!" problem which existed in .NET 1.1 with WinForms code.

Note that WinForms in .NET 2+ and WPF have slightly different models - in WPF/Silverlight, the "extra source file" isn't even present to be checked in - it's generated from the XAML as part of the build process, just before the type itself is compiled. (You can still find the generated code, typically with called SomeType.g.cs, in the obj output directory.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Am I correct in viewing partial classes like inheritance and should their use be limited to certain special cases?

Partial classes are just an organizational method. They are in no way related to inheritance, as, once compiled, they "disappear" entirely.

It's just a way to split your code up between multiple files.

That being said, I personally would only recommend using partial classes to extend a class that's being generated by a tool, such as a UI designer, a service contract, an ORM, etc. Using partial classes for your own code tends to lead to classes that are too large, and which should be refactored into separate, smaller classes that each serve one purpose.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

It is not related to OO or to any other paradigm.

And it is a fallback, it was introduced to support Tools and Designers.
For example MyForm.Designer.cs, MyDataset.Designer.cs etc.

If the code is all yours (no tools or generators) you wouldn't want to use a partial class.

H H
  • 263,252
  • 30
  • 330
  • 514
1

Partials are a compiler feature, nothing to do with OO.

In C# they simply allow you to split a class across several separate files.

It is a way to allow generated code to live with hand crafted code without one effecting the other (a simplification of the feature, I know).

I suggest reading about the feature.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

One problem with partial classes is that they complicate inheritance. If you want to have your class inherit from another class, you must change all class declarations (each partial and the public) to inherit.

I would advise never using them outside of WinForms. If you need to extend a class without inheriting, use extension methods, they are much cleaner.

tom.dietrich
  • 8,219
  • 2
  • 39
  • 56