It is a good programming habit to save all the source code as a type in a single file, but sometimes a type is too large to become an impractical constraint. In addition, programmers often use source code generators to generate the initial structure of an application and then modify the generated code. Unfortunately, when the source code is released again in the future, the existing changes will be overwritten.
Partial typesClasses, structures, and interfaces can be broken into multiple code fragments and different source code files can be found to simplify development and maintenance. In addition, partial types allows the machine to separate the types produced by the user, so that the code generated by the tool can be conveniently added.
Partial is a new type modifier that is used to define a type in multiple parts. The following is an example of a partial class, which is implemented in two parts. These two parts may be in two different source code files. For example, the first part is generated by a database ing tool, and the second part is manually written.
Public partial Class Customer
{
Private int ID;
Private string name;
Private string address;
Private list <order> orders;
Public customer (){
}
}
Public partial Class Customer
{
Public void submitorder (order ){
Orders. Add (order );
}
Public bool hasoutstandingorders (){
Return orders. Count> 0;
}
}
When the two parts are compiled together, the Code produced by the result is as if this class was written in a unit.
Public Class Customer
{
Private int ID;
Private string name;
Private string address;
Private list <order> orders;
Public customer (){
}
Public void submitorder (order ){
Orders. Add (order );
}
Public bool hasoutstandingorders (){
Return orders. Count> 0;
}
}
All parts of a partial type must be compiled together so that all parts can be integrated during compilation. In particular, partial types cannot be added to compiled types.