Rediger

Del via


Partial type (C# Reference)

Partial type definitions allow for the definition of a class, struct, interface, or record to be split into multiple definitions. These multiple definitions can be in different files in the same project. One type declaration contains only the signatures for partial members:

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

The other declaration contains the implementation of the partial members:

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

The declarations for a partial type can appear in either the same or multiple files. Typically, the two declarations are in different files. You split a class, struct, or interface type when you're working with large projects, or with automatically generated code such as that provided by the Windows Forms Designer or Source generators like RegEx. A partial type can contain partial members.

Beginning with C# 13, you can define partial properties and partial indexers. Before C# 13, only methods could be defined as partial members.

Documentation comments can be provided on either the declaring declaration or the implementing declaration. When documentation comments are applied to both type declarations, the XML elements from each declaration are included in the output XML. See the article on partial members for the rules on partial member declarations.

You can apply attributes to either declaration. All attributes are both declarations, including duplicates, are combined in the compiled output.

For more information, see Partial Classes and Methods.

C# language specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also