分部类型(C# 参考)partial type (C# Reference)

通过分部类型可以定义要拆分到多个文件中的类、结构或接口。Partial type definitions allow for the definition of a class, struct, or interface to be split into multiple files.

在 File1.cs 中:In File1.cs:

namespace PC
{
    partial class A
    {
        int num = 0;
        void MethodA() { }
        partial void MethodC();
    }
}

在 File2.cs 中,声明:In File2.cs the declaration:

namespace PC
{
    partial class A
    {
        void MethodB() { }
        partial void MethodC() { }
    }
}

备注Remarks

在处理大型项目或自动生成的代码(如 Windows 窗体设计器提供的代码)时,在多个文件间拆分类、结构或接口类型可能会非常有用。Splitting a class, struct or interface type over several files can be useful when you are working with large projects, or with automatically generated code such as that provided by the Windows Forms Designer. 分部类型可以包含分部方法A partial type may contain a partial method. 有关详细信息,请参阅分部类和方法For more information, see Partial Classes and Methods.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also