What is a partial class in C#?

Classes can be defined as user-defined data types representing the state (properties) and the behavior (actions) of an object.

Types of classes

Classes have four major types in C#, which are as follows:

  1. Abstract class
  2. Partial class
  3. Sealed class
  4. Static class
Types of classes in C#
Types of classes in C#

In this Answer, we’ll only discuss the partial classes.

Partial classes

The partial class is the type of class that can allow dividing its components into multiple source files and is combined into a single class at compile time. Using partial classes can have a number of benefits to the developers as different developers can work in the same class simultaneously.

Example

For example, we can have two files as follows:

  • Car.cs
  • CarMethods.cs
CarMethods.cs
Car.cs
public partial class Car //This is Car.cs
{
public Car() {
generatenumberplate();
}
public int Carid { get; set; }
partial void generatenumberplate();
}

Code explanation

  • The Car.cs contains the declaration of the partial method (a method is basically a function that is declared in one partial class but defined in the other one). generatenumberplate().
  • We declare a method generatenumberplate() in Car.cs, it is to be noted that this is only the declaration and this method will be defined in CarMethods.cs.
  • The CarMethods.cs contains the implementation of the generatenumberplate() method.
  • In the generatenumberplate() method, the Carid is initialized to a random number.
  • In the Main part of the program, we declare a variable Merc and assign a random car ID to it.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved