Reputation: 69
How can a partial class be useful in coding? Can anyone explain in detail with examples?
Upvotes: 6
Views: 1101
Reputation: 6133
I have seen people using partial classes to separate classes that are too large into separate files - where a better solution would be to split the class up into many separate classes.
I consider it an anti-pattern to use partial classes for splitting hand written classes into separate files.
Upvotes: 2
Reputation: 2158
Partial class is use full when multiple users are working on same class so keyword partial is used. but when compiler compiles the code it combine all partial classes with same name to one class.hence it reacts as one class.
Upvotes: 0
Reputation: 3880
Another use: if you have a giant tab control with many tabs that you want to split into one tab's code per file, you can do that with partials.
Upvotes: 0
Reputation: 3068
Partial classes are very useful in scenarios , when two or more developers are working on same class. If the Class is not partial then only one developer can be working on the class at a time. But in case of partial classes, you can create a number of files with same class name(full qualified name).
partial classes declaration goes like this..
public partial class MyFirstPartialClass
{
}
Upvotes: 2
Reputation: 1044
A partial class is used when you want to define a class over one or more files.
Usually this is used to separate the code that generates the UI from the code that does contains the UI's logic. C# does this for example.
Sometimes when I have a huge class that cannot be broken down in other ways I used partial classes to separate groups of methods together although I do this very very rarely.
Upvotes: 0
Reputation: 6637
Source msdn.
Have a look at this link.
Upvotes: 8
Reputation: 62256
In addition to @Jon post: it provides you possibility to destribute same class among different files. So, for example, different developers can work on same big class, without jumping into the conflicts on any source control system.
We used it often, in case of big classes, bascially Facades, to leave dev group to "breath".
Regards.
Upvotes: 1
Reputation: 64487
The most prevalent example is in code-generation. The WinForms designer as of Visual Studio 2005 does this. It allows code-generated code to go into one file, while your hand-crafted code stays in another file. They are glued together at the end by the compiler.
Upvotes: 5
Reputation: 706
This might help you out.
https://web.archive.org/web/20211020111732/https://www.4guysfromrolla.com/articles/071509-1.aspx
Upvotes: 0
Reputation: 1500875
The main use is to separate designer-generated code (e.g. a UI, or entities) from hand-written code; the two are mashed together at compile time, so you still just get a single class, but you don't see the designer cruft when looking at code.
It's not the only useful situation, but it's the main one I've come across.
Upvotes: 15