6

How can a partial class be useful in coding? Can anyone explain in detail with examples?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Sai Sankar
  • 69
  • 1

10 Answers10

15

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
8
  1. When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
  2. When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

Source msdn.

Have a look at this link.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38
5

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.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
2

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
{

}
Sumit
  • 2,932
  • 6
  • 32
  • 54
2

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.

GarethOwen
  • 6,075
  • 5
  • 39
  • 56
1

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

This might help you out.

https://web.archive.org/web/20211020111732/https://www.4guysfromrolla.com/articles/071509-1.aspx

jabroni
  • 706
  • 1
  • 7
  • 25
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard Jul 19 '11 at 12:36
  • No problem, I will keep that in mind moving forward. It makes sense to do add more than just links. – jabroni Jul 19 '11 at 12:40
0

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.

Sheik Yerbouti
  • 1,054
  • 1
  • 8
  • 14
0

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.

hoodaticus
  • 3,772
  • 1
  • 18
  • 28
0

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.

SMK
  • 2,098
  • 2
  • 13
  • 21