Partial Classes in Depth


Partial classes permit splitting a single class, interface, or struct into multiple, separate files. There are several advantages to splitting these elements into many files, including permitting more than one developer to work on the same class.
OR
partial classes mean that your class definition can be split into multiple physical files. Logically, partial classes do not make any difference to the compiler. During compile time, it simply groups all the various partial classes and treats them as a single entity.

The partial keyword also means that it’s possible to split generated code into two parts: the part that’s generated, and another part that provides a convenient location for you and me to add customizations.

One of the greatest benefits of partial classes is that it allows a clean separation of business logic and the user interface (in particular the code that is generated by the visual designer). Using partial classes, the UI code can be hidden from the developer, who usually has no need to access it anyway.

Partial classes will also make debugging easier, as the code is partitioned into separate files.

Using Partial Classes
Listing 1 contains two class definitions written in VB.NET, with the second class definition starting with the partial keyword. Both class definitions may reside in two different physical files. Functionally, Listing 1 is equivalent to Listing 2.

Listing 1
‘—File1.vb—
Public Class Class1
Public Sub method1()

End Sub
End Class

File2.vb
Partial Public Class Class1
Public Sub method2()

End Sub
End Class
Listing 2
‘—File1.vb—
Public Class Class1
Public Sub method1()

End Sub
Public Sub method2()

End Sub
End Class
So, what are the uses for partial classes?

Here are some good reasons to use partial classes:
They allow programmers on your team to work on different parts of a class without needing to share the same physical file. While this is useful for projects that involve big class files, be wary: If you find your class file getting too large, it may well signal a design fault and refactoring may be required.
The most compelling reason for using partial class is to separate your application business logic from the designer-generated code. For example, the code generated by Visual Studio 2005 for a Windows Form is kept separate from your business logic (we will discuss this in a later section). This will prevent developers from messing with the code that is used for the UI. At the same time, it will prevent you from losing your changes to the designer-generated code when you change the UI.

Rules for Partial Classes
Elements That Can Use the Partial Keyword
The partial keyword can be applied to a class, a struct (structure in VB), and an interface. All of the parts make up the whole, and you can split the parts into as many source files as you’d like.
The partial class cannot be applied to an enumeration or a delegate, although a delegate is really a class.

Defining Parts in the Same Namespace
If you define two classes with the same name in different namespaces, the result is a different class, even if each class uses the partial keyword. For multiple parts to be part of the same whole, each part must reside in the same namespace.

Assigning Accessibility
All parts must have the same accessibility modifier. For example, if one part is public, all parts should be public. If any part has the sealed modifier applied, all parts are considered sealed. (Sealed is equivalent to NotInheritable in VB.)

Specifying Generalization and Realization
Only one part needs to provide the name of any parent classes, but all parts inherit from the same parent (this is referred to as generalization). If all parts are specific to a parent class, then all of the parts must agree.
Each part can specify different interfaces—called realization—but in the end the final type must implement (or realize the union of) all of the interfaces defined by thee parts.

Finally, the partial keyword must precede one of the three words class, struct, or interface in C#, or Class, Structure, or Interface in Visual Basic.NET. All parts must reside in the same module or assembly (.exe or .dll).

*Generic can be defined as partial classes, but the class name and all generic parameters must match on all of the parts.*

Advertisements

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s