Skip to main content

How to Split a Class Across Files with partial in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A partial class is written in pieces: one file for its data, another for its methods, and the compiler joins the pieces into one class. It's how generated code and your own code can share a class without overwriting each other.

Microsoft's reference: Partial type definitions allow you to split the definition of a class, struct, interface, or record into multiple definitions. You can put these multiple definitions in different files within the same project.

Syntax​

Syntax
// One file
public partial class <Name>
{
// some members
}

// Another file
public partial class <Name>
{
// more members
}
PartWhat it is
partialOn every part: tells the compiler the class continues elsewhere
<Name>The same class name in every part
publicThe same access modifier on every part

The rules, from Microsoft's guide: All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

The Example​

A console app called PartialClass splits a Ticket class into two files: Ticket.cs holds its data and Ticket.Summary.cs a method that describes it.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n PartialClass
cd PartialClass
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/PartialClass/PartialClass.csproj:
Determining projects to restore...
Restored /Users/you/source/PartialClass/PartialClass.csproj (in 46 ms).
Restore succeeded.

dotnet new console -n PartialClass creates a folder named PartialClass with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or PartialClass.csproj in Visual Studio.

Step 2: Write the Code​

Create a file named Ticket.cs next to Program.cs, for the data:

Ticket.cs
// The first part of Ticket: its data.
public partial class Ticket
{
public string Id { get; set; } = "";
public string Title { get; set; } = "";
}

Then a file named Ticket.Summary.cs, for the method:

Ticket.Summary.cs
// The second part of Ticket. The compiler joins both parts into one class.
public partial class Ticket
{
public string Summary() => $"{Id}: {Title}";
}

Summary reads Id and Title, which are declared in the other file: to the compiler, both parts are one class. The file names are up to you; the compiler joins the parts by the class name.

Last, replace everything in Program.cs:

Program.cs
Ticket laptop = new() { Id = "INC1001", Title = "Laptop won't start" };
Ticket vpn = new() { Id = "INC1002", Title = "VPN keeps disconnecting" };

Console.WriteLine(laptop.Summary());
Console.WriteLine(vpn.Summary());

new() { Id = ..., Title = ... } creates a ticket and sets its two properties.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
INC1001: Laptop won't start
INC1002: VPN keeps disconnecting

macOS-style terminal window titled Terminal — PartialClass showing dotnet run printing INC1001: Laptop won't start and INC1002: VPN keeps disconnecting

Both tickets printed their summary: a method from one file, using properties from the other.

The Complete Files​

macOS-style code window titled Program.cs — PartialClass showing two tickets created with their Id and Title and two Console.WriteLine calls that print their Summary

Program.cs
Ticket laptop = new() { Id = "INC1001", Title = "Laptop won't start" };
Ticket vpn = new() { Id = "INC1002", Title = "VPN keeps disconnecting" };

Console.WriteLine(laptop.Summary());
Console.WriteLine(vpn.Summary());

macOS-style code window titled Ticket.cs — PartialClass showing the first part of the public partial class Ticket, with the Id and Title properties

Ticket.cs
// The first part of Ticket: its data.
public partial class Ticket
{
public string Id { get; set; } = "";
public string Title { get; set; } = "";
}

macOS-style code window titled Ticket.Summary.cs — PartialClass showing the second part of the public partial class Ticket, with a Summary method that returns the Id and the Title

Ticket.Summary.cs
// The second part of Ticket. The compiler joins both parts into one class.
public partial class Ticket
{
public string Summary() => $"{Id}: {Title}";
}

Common Mistakes​

MistakeWhat happensDo this instead
Leaving partial off one parterror CS0260: Missing partial modifier on declaration of type 'Ticket'; another partial declaration of this type existsWrite partial on every part
Different access modifiers on the partserror CS0262: Partial declarations of 'Ticket' have conflicting accessibility modifiersUse the same one, such as public, on every part
Declaring the same member in two partserror CS0102: The type 'Ticket' already contains a definition for 'Id'Declare each member in one part only

FAQ​

Can the parts be in different projects?​

No. Microsoft's guide: All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file).

Where will I meet partial classes?​

In generated code. Microsoft's reference lists automatically generated code such as that provided by the Windows Forms Designer, or Source generators like RegEx: the generator writes its part of your class in a file of its own.

Conclusion​

Mark every part partial, give them the same name and access modifier, and keep them in the same project: the compiler builds one class from all of them. Each member still lives in exactly one part.

Additional Resources​