13

I'm asking this because I find it quite a dangerous feature to distribute the class definition so that you can't really be sure if you know all about it. Even if I find three partial definitions, how do I know that there's not a fourth somewhere?

I'm new to C# but have spent 10 years with C++, maybe that's why I'm shaken up?

Anyway, the "partial" concept must have some great benefit, which I'm obviously missing. I would love to learn more about the philosophy behind it.

EDIT: Sorry, missed this duplicate when searching for existing posts.

Community
  • 1
  • 1
sharkin
  • 12,162
  • 24
  • 86
  • 122
  • "how do I know that there's not a fourth somewhere?" The problem here is not partial classes, but a lack of knowledge of the project you are compiling. – mackenir Sep 18 '09 at 09:57
  • http://stackoverflow.com/questions/612831/practical-usage-of-partial-keyword-in-c – Chris S Sep 18 '09 at 10:15
  • @mackenir: Try to pitch that argument to a maintenance developer assigned to a 100000+ lines of code project. – sharkin Sep 18 '09 at 13:53
  • 2
    The "go to definition" feature in Visual Studio will give you a list of the, say, four places that a partial type is declared. – Eric Lippert Sep 18 '09 at 14:57

6 Answers6

31

Partial classes are handy when using code generation. If you want to modify a generated class (rather than inheriting from it) then you run the risk of losing your changes when the code is regenerated. If you are able to define your extra methods etc in a separate file, the generated parts of the class can be re-created without nuking your hand-crafted code.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
11

The great benefit is to hide computer generated code (by the designer).
Eric Lippert has a recent blog post about the partial-keyword in general.

Another usage could be to give nested classes their own file.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
tanascius
  • 53,078
  • 22
  • 114
  • 136
3

Two people editing the same class, and autogenerated designer code are the two immediate features I can see that were solved by partial classes and methods.

Having designer generated code in a separate file is a lot easier to work with compared to 1.1, where you code could often be mangled by Visual Studio (in windows forms).

Visual Studio still makes a mess of syncing the designer file, code behind and design file with ASP.NET.

Chris S
  • 64,770
  • 52
  • 221
  • 239
  • Two people editing the same class should use version control, not partial classes. – Svante Svenson Sep 18 '09 at 10:04
  • Or both, and save a merge headache if it's some huge legacy class with 1000s of lines. – Chris S Sep 18 '09 at 10:14
  • I'm not suggesting it's the right way to work, but that was one of the ideas behind partial classes: http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx – Chris S Sep 18 '09 at 10:20
3

An other point is, that when a class implements multiple interfaces, you can split the interface implementations on diffrent files.

So every code file has only the code that belongs to the interface implementation. It´s according to separation of concerns concept.

Jehof
  • 34,674
  • 10
  • 123
  • 155
2

If you have some kind of absurdly large class that for some reason are unable or not allowed to logically break apart into smaller classes then you can at least physically break them into multiple files in order to work with it more effectively. Essentially, you can view small chunks at a time avoiding scrolling up and down.

This might apply to legacy code that perhaps due to some arcane policy are not allowed to mess with the existing API because of numerous and entrenched dependencies.

Not necessarily the best use of partial classes, but certainly gives you an alternate option to organize code you might not be able to otherwise modify.

Ray
  • 187,153
  • 97
  • 222
  • 204
1

maybe its too late but please let me to add my 2 cents too:

*.When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.

*.You can easily write your code (for extended functionality) for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code

siamak
  • 669
  • 1
  • 10
  • 28