Sai Sankar
Sai Sankar

Reputation: 69

What is the use of a partial class?

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

Upvotes: 6

Views: 1101

Answers (10)

GarethOwen
GarethOwen

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

SMK
SMK

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

hoodaticus
hoodaticus

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

Sumit
Sumit

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

Sheik Yerbouti
Sheik Yerbouti

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

FIre Panda
FIre Panda

Reputation: 6637

  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.

Upvotes: 8

Tigran
Tigran

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

Adam Houldsworth
Adam Houldsworth

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

Jon Skeet
Jon Skeet

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

Related Questions