views:

411

answers:

6

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.

+10  A: 

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
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
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
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
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
+3  A: 

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

Mitch Wheat
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
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
A: 

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
A: 

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
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
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.).
Groo
+3  A: 

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.

Kevin
+1  A: 

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