C partial keyword description

  • 2021-09-11 20:58:25
  • OfStack

In C #, you can split the definition of a class, structure, or interface into two or more source files by adding the partial keyword before the class declaration.

1. What is a local type?

C # 2.0 introduces the concept of local types. Local types allow us to divide a class, structure, or interface into several parts, which are implemented in several different. cs files.

Local types are suitable for the following situations:

(1) The type is very large, so it is not suitable to put it in one file.
(2) One part of the code in one type is generated by automation tools, which should not be mixed with the code written by ourselves.
(3) It is necessary for many people to cooperate to write a class.

Local types are a pure language layer compilation process that does not affect any execution mechanism-in fact, the C # compiler will still merge the local types of various parts into a complete class when compiling.


public partial class Program
{
static void Main(string[] args)
{
}
}
partial class Program
{ 
public void Test()
{ 
}
}

2. Restrictions on local types

(1) Local types are only applicable to classes, interfaces and structures, and do not support delegates and enumerations.

(2) Each part of the same type must have the modifier partial.

(3) When using local types, all parts of a type must be in the same namespace.

(4) All parts of a type must be compiled at the same time.

3. Attention points of local types

(1) The keyword partial is a context keyword, which has the meaning of keyword only when it is placed with class, struct and interface. Therefore, the introduction of partial does not affect the variable named partial in the existing code.

(2) Parts 1 of a local type are generally split into several different. cs files, but the C # compiler allows us to put them in the same file.

4. Application characteristics of local types

Characteristics on local types have "accumulation" effect.


[Attribute1, Attribute2("Hello")]
partial class Class1{}
[Attribute3, Attribute2("Exit")]
partial class Class1{}

Equivalent to


[Attribute1, Attribute2("Hello"), Attribute3, Attribute2("Exit")]
class Class1 {}

Note: The Attribute2 attribute allows multiple uses on the class.

5. Modifiers on local types

(1) Access modifiers on various parts of a type must remain unisex.
(2) If one part of a type uses the abstract modifier, the entire class is treated as an abstract class.
(3) If one part of a type uses the sealed modifier, the entire class is treated as a sealed class.
(4) You can't use contradictory modifiers in parts of a class, such as abstract on one part and sealed on another.

6. Base classes and interfaces for local types

(1) The base class specified on each part of a type must be 1. A part may not specify a base class, but if it does, it must be the same.
(2) Interfaces on local types have an "accumulation" effect.


partial class Class2: Iinterface1, Iinterface2 {}
partial class Class2: Iinterface3 {}
partial class Class2: Iinterface2 {}

Equivalent to


class Class2: Iinterface1, Iinterface2, Iinterface3 {}

Saving all source code as a type in a single file is a good programming practice, but sometimes a type becomes so large that doing so becomes 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 at some point in the future, the existing changes will be overwritten.

Partial types allows classes, structures, and interfaces to be broken into multiple code fragments that exist in different source code files for ease of development and maintenance. In addition, partial types allows machine-generated and user-written types to be partially separated, making it easy to add tool-generated code.

Partial, a new type modifier, is used when defining a type in multiple parts. The following is an example of an partial class, 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 mapping tool, and the second part is written by hand.


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 order) {
orders.Add(order);
}
public bool HasOutstandingOrders() {
return orders.Count > 0;
}
}

When the above two parts are compiled in 1, the resulting code is as if the class is written in 1 unit.


public class Customer
{
private int id;
private string name;
private string address;
private List<Order> orders;
public Customer() {
}
public void SubmitOrder(Order order) {
orders.Add(order);
}
public bool HasOutstandingOrders() {
return orders.Count > 0;
}
}

All parts of an partial type must be compiled in 1, so that all parts can be integrated in 1 at compilation time. In particular, partial types does not allow the addition of compiled types.

The above is the keyword description of C # partial introduced by Xiaobi, hoping to help everyone!


Related articles: