C# Partial Class

The c# partial class is used to split a class into two or more physical files. Although the class resides in multiple physical files, after the compilation, these files get together and creates a single file again.

In the same way, the partial keyword can be used to split the interfaces and structures into multiple files.

Let's understand the partial class with an example.

Following is the Student partial class which physical file name is PartialStudentOne.cs. This class has properties, class fields, and two constructor methods.

public partial class Student
{
    string _firstName = string.Empty;
    string _lastName = string.Empty;
    private string _fullName = string.Empty;
    int _age = 0;
 
    public string FirstName { get; set; }
    public string LastName { get; set; }
 
    public Student()
    {
 
    }
 
    public Student(string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
    }
}

Try It

Following is another Student partial class which has a method having two parameters which print the firstName and lastName of a student. The physical file name for following class is PartialStudentTwo.cs

public partial class Student
{
    public void PrintStudentDetail(string firstName, string lastName)
    {
        Console.WriteLine("Student first name is : {0} and student last name is : {1}", firstName, lastName);
    }
}

In the following example, there is a simple program which is performing different tasks. First, it is assigning values to the FirstName, LastName properties then it is calling the PrintStudentDetail method of Student class.

Example

Try It
public static void Main()
{
    Student student = new Student();
    student.FirstName = "Jhon";
    student.LastName = "Doe";
 
    student.PrintStudentDetail();
}
//Output
//Student first name is : Jhon and student last name is : Doe

If you will notice above the Sample program is using properties and method of Student class, despite of that both exists in different physical files. Following are the important rules which needs to be followed while creating partial classes.

Important Rules

  1. All the physical files which have been used to create a partial class must use a partial keyword while declaring a class.
  2. If any of the parts of a partial class is using sealed keyword then the entire partial class becomes sealed, similarly, it becomes abstract if any part uses abstract keyword.
  3. All parts of a partial class must use the same access modifier.
  4. It's important to understand that C# does not support multiple class inheritance concept, So if any part of a partial class is inheriting a base class then other parts can not inherit another base class.

Example:

public class StudentBase
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
 
//Physical file name StudentOne.cs
public partial class Student : StudentBase // Inheriting StudentBase class in Student class
{
    public int Age { get; set; }
}
 
//Physical file name StudentTwo.cs
public partial class Student : StudentBase //Can't inherit another class again
{
 
}

In the above example, there are two physical files StudentOne.cs and StudentTwo.cs Both have a partial class Student. Inside StudentOne.cs the Student class is inheriting the StudentBase class. Due to that fact, the class inside StudentTwo.cs can't inherit the Base class again.

Although a partial class can implement multiple interfaces and their implementation can be placed inside any part of the partial class.

//Physical file name StudentOne.cs
partial class Student : IStudent
{
    public int FirstName { get; set; }
    public int LastNameName { get; set; }
 
    void PrintSubjects() //Perfectly valid to implement the IStudent interface in another physical file
    {
        //Print Subject
    }
}
 
//Physical file name StudentTwo.cs
partial class Student : ISubject
{
    public int Age { get; set; }
 
    public void PrintName()  //Perfectly valid to implement the IStudent interface in another physical file
    {
        //Print Name
    }
}
 
public interface IStudent
{
    void PrintName();
}
 
interface ISubject
{
    void PrintSubjects();
}