C #2.0 incomplete type

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.