19.1 Partial Types
While It Is Good Programming Practice To Maintain All Source Code for a Type In a Single File, Sometimes a Type Becomes Large Enough That this is an impleractical constraint.
FURTH
ermore, programmers often use source code generators to produce the initial structure of an application, and then modify the resulting code. Unfortunately, when source code is emitted again sometime in the future, existing modifications are overwritten.
Partial types allow classes, structs, and interfaces to be broken into multiple pieces stored in different source files for easier development and maintenance. Additionally, partial types allow separation of machine-generated and user-written parts of types so that it is easier to augment Code generated by a Tool.
A new type modifier, partial, is used when defining a type in multiple parts. The following is an example of a partial class that is implemented in two parts. The two parts may be in different source files, for example because the first part is Machine generated by a database mapping Tool and the second part is manually authored:
Public Partial Class Customer {Private INT ID; Private String Name; Private list
Public Customer () {...}}
Public Partial Class Customer {Public Void Submitorder (Order Order) {Orders.Add (Order);
Public Bool HasoutStandingRDERS () {Return ORDERS.COUNT> 0;}}
When the Two Parts Above Are Compiled Together, The Resulting Code Is The Same Asia A Single Unit:
Public class customer {private int ID; private string name; private list
Public Customer () {...}
Public void submitorder (Order Order) {Orders.Add (Order);} public bool HasoutstandingRDERS () {return Orders.count> 0;}}
All Parts of a Partial Type Must Be Compiled Together Such That The Parts CAN Be Merged At Compile-Time. Partial Types Specification Types To Be Extended.