Home > Backend Development > C#.Net Tutorial > C# 2.0 Sepcification(3)

C# 2.0 Sepcification(3)

黄舟
Release: 2017-01-03 10:16:12
Original
1579 people have browsed it

(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 List<Order> orders;
public Customer()
{
…
}
}
public partial class Customer
{
public void SubmitOrder(Order order)
{
orders.Add(order);
}
public bool HasOutstandingOrders()
{
return orders.Count>0;
}
}
Copy after login

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 List<Order> orders;
public Customer()
{
…
}
public void SubmitOrder(Order order)
{
orders.Add(order);
}
public bool HasOutstandingOrders()
{
return orders.Count>0;
}
}
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template