Home>Article>Backend Development> C# 2.0 Sepcification(3)
(Continued)
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 Get very large and this becomes an unrealistic limit. In addition, programmers often use source code generators to generate the initial structure of an application and modify the resulting code. Unfortunately, when the source code is re-emitted in the future, existing modifications will be overwritten.
Incomplete types (partial types) allow classes, structures and interfaces to be split into multiple parts and stored in different source files, which is more conducive to development and maintenance. In addition, incomplete types allow separation between machine-generated and user-written parts of certain types, so augmenting the code produced by the tool is easy.
When defining a type in multiple parts, you can use a new type modifier partial. Below is an example of an incomplete class that is implemented in two parts. The two parts can be in different source files, for example, because the first part is machine-generated via a database mapping tool and the second part is created by hand.
public partial class Customer { private int id; private string name; private string address; pivate Listorders; 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 resulting code is the same as the class written as a single unit.
public class Customer { private int id; private string name; private string address; pivate Listorders; 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 fused together at compile time. In particular, incomplete types do not allow extensions to already compiled types.
The above is the content of C# 2.0 Sepcification (3). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!