9

I have been reading Programming Microsoft® Visual C#® 2008: The Language to get a better understanding of C# and what can be done with it. I came across partial classes which I had already encountered from ASP.Net's Page class.

To me it seems that you can do what partial classes do with an abstract class and an overridden one. Obviously one team will be in control of the interface via the abstract methods but you would be relying on each other anyway. And if the goal is collaboration then isn't that what source control and other tools solve.

I am just missing the point to a partial class. Also could someone provide a real world use.

uriDium
  • 13,110
  • 20
  • 78
  • 138

8 Answers8

22

Partial classes have nothing to do with object inheritance. Partial classes are just a way of splitting the source code that defines a class into separate files (this is for example done when you create a new form in your Windows Forms application - one file is "your" code, another file .designer.cs contains the code that VS2008 manages for you).

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I know that it has nothing to do with inheritance BUT you can do the same thing because it is a sort of side effect of inheritance that you can split code into separate files. Probably not as nicely or cleanly as partial classes offers though. I am just trying to find a use for them. – uriDium Jun 29 '09 at 11:45
  • Oops, sorry. I was too quick on the keyboard. So ultimately the designer code and your code are going to end up in the same file? Now I see the use if that is the case. But then I will probably never use it unless I am trying to do something similar to that. – uriDium Jun 29 '09 at 11:47
  • They do something quite different to inheritance. An example of the use is Visual Studio. The form builder generates a partial class file with the code that sets up the controls. The user code lives in a separate file. When compiled these files merge together into a single class. The really killer app of Partial classes is to produce classes that consist partially but not wholly of generated code. You could do something similar with (for example) an O/R mapper. – ConcernedOfTunbridgeWells Jun 29 '09 at 11:48
  • Yes, the compiler sees those separate files as a single class definition (I guess you could imagine that it merges them into one single file in memory before compiling). An abstract class however is sort of like a "template" for other classes in that its definition is complete, but the implementation is not. An abstract class however can also be implemented as a partial class, if you like to spread the class definition across several files. – Thorsten Dittmar Jun 29 '09 at 11:49
4

A good usage example is when one side of the partial class is generated (such as an ORM)

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    IMO, this is the only good usage of partial classes. Having to split up a single class into multiple files is a smell. – Jeremy Frey Jun 29 '09 at 11:50
  • IMO this is one of the bad usages of partial classes. If the generated class was abstract you could override behaviour, validation on setters etc. With patial classes you are stuck with the methods/properties the generated side produces. – David Waters Jun 29 '09 at 12:42
4

The great thing about a partial class is that you can take an existing class and add on to it. Now this sounds a lot like inheritance, but there are a lot of things that inheritance can't do that partial classes will.

Here's one think about the Linq to SQL classes generated for you. They are autogenerated meaning you shouldn't modify them. Without a partial class, you can't attach an interface. You could create a new class and derive that from the Linq to sql class, but that really doesn't get you anything because you can't upcast the linq to sql class to your class with the interface.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
2

Partial classes should be restricted to using with auto-generated code, where the other code cannot be modified. Using it as a substitute for inheritance or adding functionality are not best practices.

If you have a large class, its already wrong. Code should be refactored into multiple "real" classes instead of multiple files. Large classes in general signifies the class is doing too many things and violates SRP (Single Responsibility Principle).

vasya10
  • 599
  • 6
  • 19
1

Partial class are now used heavily in ASP.Net to allow two source files the mark-up based example.aspx and the code based example.aspx.cs so that methods and variable defined in each are visible to each. in the example.aspx

<custom:exampleControl id="exampleCntr" property="<%#getProperty()%>" />

in the example.aspx.cs

private object GetProperty(){ // called from aspx
    return DateTime.Now;
}

private void DoStuff(){
    ExampleControl c = exampleCntr; //exampleCntr is defined in aspx.
}

The bi-directional nature of this cannot be recreated with abstract classes.

David Waters
  • 11,979
  • 7
  • 41
  • 76
1

It sounds like your question is what the difference is between

partial class Foo
{
  PART ONE
}
partial class Foo
{
  PART TWO
}

and

astract class FooBase
{
  PART ONE
}
partial class Foo : FooBase
{
  PART TWO
}

While they may appear somewhat similar, and in some cases the latter construct could be used in place of the former, there are at least two problems with the latter style:

-1- The type FooBase would likely have to know the identity of the concrete type which was supposed to derive from it, and always use variables of that type, rather than of type FooBase. That represents an uncomfortably-close coupling between the two types.

-2- If type Foo is public, type FooBase would have to also be public. Even if all the constructors of FooBase are internal, it would be possible for outside code to define classes which derived from FooBase but not Foo; constructing instances of such classes would be difficult, but not impossible.

If it were possible for a derived type to expand the visibility of a base type, these issues wouldn't be overly problematical; one would regard FooBase as a "throwaway" identifier which would appear exactly twice: once in its declaration, and once on the declaration line for Foo, and figure that every FooBase would be a Foo in disguise. The fact that FooBase could not use Foo instance members on this without a typecast could be irksome, but could also encourage a good partitioning of code. Since it isn't possible to expand the visibility of a base type, however, the abstract-class design seems icky.

supercat
  • 77,689
  • 9
  • 166
  • 211
0

Purpose of partial classes is to allow a class's definition to span across multiple files. This can allow better maintainability and separation of your code.

Perica Zivkovic
  • 2,610
  • 24
  • 32
0

We use partial classes to split up our larger classes. That way it's easier to check out a part of the code with Sourcesafe. This limits the cases where four developers need to access the same file.

Carra
  • 17,808
  • 7
  • 62
  • 75
  • While I know about partial classes, I've actually never used them myself (apart from auto-generated ones). Are there team-internal rules as to what type of method goes into what kind of file? Is there a naming convention? I've always been a bit worried about increasing the "find a certain code portion"-time when using partial classes extensively. – Thorsten Dittmar Jun 29 '09 at 11:47
  • I find it useful when creating custom Controls. Then you often have lots of methods and properties to override in a single class, so we split it into logical groups (e.g. "control.Input.cs", "control.Draw.cs", "control.Properties", etc.). – vgru Jun 29 '09 at 11:53