C # 2.0 Sepcification (iii)

Source: Internet
Author: User
(connected)
19.4 Incomplete types
Although it is a good programming practice to maintain all source code for a type in a single file, sometimes a type becomes very large, which becomes an unrealistic limitation. In addition, programmers often use source code generators to generate the initial structure of the application and modify the result code. Unfortunately, existing modifications will be overwritten when the source code is launched again in the future.

An incomplete type (partial type) allows classes, structs, and interfaces to be split into parts stored in different source files, which is more conducive to development and maintenance. In addition, incomplete types allow the separation of some types of machine-generated parts from user-written parts, so it is easy to increase the code generated by the tool.

When you define a type in more than one section, you can use a new type modifier partial. The following is an example of an incomplete class, which is implemented in two sections. These two parts can be in different source files, for example, because the first part is generated by a machine through a database mapping tool, and the second part is created manually.

public partial class Customer

{

private int id;

private string name;

private string address;

Pivate list<order> orders;

Public Customer ()

{

...

}

}

public partial class Customer

{

public void SubmitOrder (order order)

{

Orders. ADD (order);

}

public bool Hasoutstandingorders ()

{

return orders. count>0;

}

}



When the previous two sections are compiled together, the resulting code is the same as the class that is written as a single unit.

public class Customer

{

private int id;

private string name;

private string address;

Pivate list<order> orders;

Public Customer ()

{

...

}

public void SubmitOrder (order order)

{

Orders. ADD (order);

}

public bool Hasoutstandingorders ()

{

return orders. count>0;

}

}



All parts of an incomplete type must be compiled together so that the parts can be merged together at compile time. Of particular note is that incomplete types do not allow extensions to types that have been compiled.


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.