Partial Classes in C#

Last Updated : 22 Sep, 2025

In C#, a partial class allows the definition of a single class to be split into multiple files. At compile time, all the parts are combined into one complete class. This feature is especially useful in large projects where separating auto-generated code from developer-written code makes the program easier to manage and maintain.

Partial classes are commonly used in scenarios such as Windows Forms, ASP.NET applications and code generators where some parts of the class are system-generated and others are written by the programmer.

Syntax

public partial class ClassName{

// code

}

Key Points

  • Declared using the partial keyword.
  • A class can be split across multiple files.
  • All parts must use the same name and namespace.
  • At compile time, the compiler merges all parts into a single class.
  • Useful for separating auto-generated and user-defined code.
  • Supports defining fields, methods, properties, events and nested classes.

Example: Here, we are taking a class named Geeks and split the definition of Geeks class into two different files named Geeks1.cs and Geeks2.cs as shown below:

partial_classes_in_c_
Partial Class in C#

In Geeks1.cs and Geeks2.cs, a partial class is created using the partial keyword and each file contains different functionality of Geeks class as shown below.

Geeks1.cs

C#
public partial class Geeks {
    private string Author_name;
    private int Total_articles;

    public Geeks(string a, int t)
    {
        this.Author_name = a;
        this.Total_articles = t;
    }
}

Geeks2.cs

C#
public partial class Geeks {
    public void Display()
    {
        Console.WriteLine("Author's name is : " + Author_name);
        Console.WriteLine("Total number of articles is : " + Total_articles);
    }
}

When we execute the above code, the compiler combines Geeks1.cs and Geeks2.cs into a single file, i.e. Geeks as shown below.
The Geeks class may contain the Main Method. For simplicity, here Main() method is not included. 

C#
public class Geeks {
    private string Author_name;
    private int Total_articles;

    public Geeks(string a, int t)
    {
        this.Authour_name = a;
        this.Total_articles = t;
    }

    public void Display()
    {
        Console.WriteLine("Author's name is : " + Author_name);
        Console.WriteLine("Total number articles is : " + Total_articles);
    }
}

Advantages

  • Partial classes allow multiple developers to work on the same class in different files.
  • They help separate UI design code from business logic, making code easier to read.
  • Auto-generated code can be added without recreating source files.
  • Large classes can be maintained better by splitting them into smaller parts.

Limitations

  • All parts must be in the same assembly and namespace
  • Must use the same access modifier (public, internal, etc.)
  • No cross-language support (all parts must be in C#)
  • Can inherit from only one base class and members cannot be split across files
Comment
Article Tags:

Explore