4

The biggest argument I've seen so far for partial classes is in the case of auto-generated code.

From a Java perspective, I don't see why this can't simply be done using abstract classes, can't the auto-generated code simply be an abstract class with protected abstract methods that it expects the user to override?

Aside from the auto-generated code cases, every other case I saw was either extremely rare (and the coders were just using partial as a hack), or it can also be solved using abstract or some other concept that already exists.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Morad
  • 734
  • 5
  • 14
  • 5
    Abstract classes rely on inheritance; partial classes do not. – BoltClock Dec 03 '13 at 08:40
  • 1
    With partial classes you can specify the base class in the hand-written part. Which is a minor advantage, but it is one. – Stefan Steinegger Dec 03 '13 at 08:41
  • 4
    *Not* opinion-based. The question can be answered with verifiable statements about the differences between `abstract` and `partial`. – O. R. Mapper Dec 03 '13 at 08:44
  • @O.R.Mapper I'm pretty close-vote-trigger-happy, but I don't see any opinion in here, yet three people voted for that... – CodeCaster Dec 03 '13 at 08:46
  • @CodeCaster: Hence my comment. Before two more people decide to vote for that without considering the counter-arguments, thus unnecessarily closing this question before it has received its deserved answer. – O. R. Mapper Dec 03 '13 at 08:47
  • @BoltClock'saUnicorn and why is that a problem? – Morad Dec 03 '13 at 09:09
  • 1
    Because if there are more than two parts to a partial class, how would you combine the functionality of all of them using inheritance alone, given that inheritance only works one way going from one type to another? – BoltClock Dec 03 '13 at 09:13
  • http://stackoverflow.com/a/2477848/2448070 "If you have three or more partial fragments for the same class, it's almost a guarantee that you're abusing partial" – Morad Dec 03 '13 at 09:21

2 Answers2

8

Inheritance is observable on the compiled assembly while partial is not. This makes inheritance unsuitable for libraries where you need to control the surface area tightly. The .NET BCL could never use this for example.

It is also a hack because inheritance is not meant to stitch together classes. It is primarily meant as a means to create substitutability and abstraction.

partial is meant for exactly this use case. The parts of a partial class can refer to each other which is not possible with inheritance.

Partial methods allow you to create methods that might exist in the compiled result, or not. LINQ to SQL uses them to give you callback hooks into various parts of the DataContext and entity classes. If you don't use them there is no performance cost at all because partial methods that are declared but never defined are just deleted.

usr
  • 168,620
  • 35
  • 240
  • 369
  • "inheritance is not meant to stitch together classes" most examples I can think of, one could find a nicer solution by thinking a bit harder about the Cohesion and Coupling of those classes, instead of putting `partial` on them and stitching them all together – Morad Dec 03 '13 at 09:07
  • 1
    partial is also not meant to stitch together unrelated stuff. It it meant to stitch *related* stuff together. Anything else is a misuse of both inheritance as well as partial classes. Some classes are just big by design, for example WinForms.Control, EF's ObjectContext and big WinForms Form classes. – usr Dec 03 '13 at 09:09
  • @usr: Or some grow big because they implement (or contain nested types that implement) one of the standard collection interfaces with loads of methods, to provide for a good compatibility with methods from other sources. – O. R. Mapper Dec 03 '13 at 09:47
0

For me, partial classes is a way to devide classes into more logical parts. Sometimes cause they are partly auto generated, sometimes because they are really complex.

Abstract is for inheritage, and why use that when nothing is really "shared".

Magnus A
  • 83
  • 1
  • 1
  • 5
  • 1
    _"partial classes is a way to devide classes into more logical parts"_ - you really should look into abstraction and composition. – CodeCaster Dec 03 '13 at 08:50