C # 2.0 Sepcification (3)

zhaozj2021-02-16  46

(Connected)

19.4 incomplete type

Although it is a good programming practice for a type to maintain all source code in a single file, but sometimes a type is very large, which will become an unrealistic limit. In addition, programmers often use the source code generator to generate the initial structure of the application, and modify the result code. Unfortunately, existing modifications will be overwritten when the source code is transmitted again in the future.

Incomplete Type (Partial Type) allows classes, structures, and interfaces to be split into multiple parts in different source files, which is more beneficial to development and maintenance. In addition, the incomplete type allows some types of machines to be separated from parts that are written in the user, so it is easy to increase the code generated by the tool.

When a type is defined in multiple sections, you can use a new type modifier Partial. Below is an example of an incomplete class, it is implemented in two parts. These two parts can be in different source files, for example, because the first part is generated by a database mapping tool, and the second portion is created by manual.

Public Partial Class Customer

{

Private int ID;

PRIVATE STRING NAME;

PRIVATE STRING ADDRESS

Pivate List 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 parts are compiled together, the result code and the class written as a single unit are the same.

Public Class Customer

{

Private int ID;

PRIVATE STRING NAME;

PRIVATE STRING ADDRESS

Pivate List ORDERS;

Public Customer ()

{

...

}

Public Void Submitorder (Order Order)

{

ORDERS.ADD (Order);

}

Public Bool HasoutStandingOrders ()

{

Return ORDERS.COUNT> 0;

}

}

All parts of the incomplete type must be compiled together, so that each part can be fused together when compiling. It is important to note that incomplete types do not allow for expansion of types that have been compiled.

转载请注明原文地址:https://www.9cbs.com/read-25906.html

New Post(0)