Partial Class in C# with Examples

  By : Suresh Dasari
  Posted On : 23-Apr-2023

Here, we will learn what is a partial class in c#, how to define and use partial classes in c#, and what are rules we need to follow to create partial classes in c# with examples.

What is Partial Class? 

As discussed in the C# Tutorial, splitting the functionality of a particular class into multiple class files using the partial keyword is called a partial class in c#.

 

Generally, partial classes are useful when multiple developers want to work on the same class file at the same time. In larger projects, the partial classes will provide the ability for multiple developers to work on the same class file simultaneously.

 

To create the partial classes in c#, we need to define the multiple class files with the same class name using the partial keyword. During the application compilation time, the compiler will combine all the partial classes into a single class.

 

The following diagram will illustrate the implementation of partial classes in c#.

 

C# Partial Class Implementation Diagram

Partial Class Syntax

As discussed, to create the partial classes we need to define the classes with partial keyword. Following is the syntax of defining the partial class in c#.

 

//File1.cs
public partial class Products
{
   // Implementation Here
}

//File2.cs
public partial class Products
{
   // Implementation Here
}

If you observe the above partial classes syntax, we split the Products class functionality into two different class files (File1.cs, File2.cs) using partial keyword.

 

During the compilation time, the compiler will combine partial class files (File1.cs, File2.cs) into a single Products class as shown below.

 

public class Products
{
   // Combined code here
}

For example, I have Products class with properties, constructors, and methods as shown below.

 

using System;

namespace Tutlane
{
   public class Products
   {
      private string Name;
      private int Price;
      public Products(string a, int b)
      {
         this.Name = a;
         this.Price = b;
      }
      public void GetDetails()
      {
         Console.WriteLine("Name: " + Name);
         Console.WriteLine("Location: " + Price);
      }
   }
}

We can split the functionality of this Products class into two class files using the partial keyword. For that, create File1.cs and File2.cs class files in your application and split the functionality of Products class based on your requirements and delete the Products class file from the project.

File1.cs

using System;

namespace Tutlane
{
  public partial class Products
  {
    private string Name;
    private int Price;
    public Products(string a, int b)
    {
      this.Name = a;
      this.Price = b;
    }
  }
}

 If you observe the above code, we created Products partial class in File1.cs class file using a partial keyword with required properties and constructor. Here, this keyword represents the current instance of the class.

File2.cs

using System;

namespace Tutlane
{
   public partial class Products
   {
      public void GetDetails()
      {
        Console.WriteLine("Name: " + Name);
        Console.WriteLine("Location: " + Price);
      }
   }
}

If you observe the above code, we created Products partial class in File2.cs class file using partial keyword with the required method.

Rules to Define Partial Class

To create partial classes in c#, we need to follow the rules. Following are the rules that we need to follow while implementing the partial classes in c#.

 

  1. We need to use the partial keyword to split the functionality of a particular class into multiple class files and all the class files must be available during the compile time to combine all the partial classes into a single class file.
  2. All sections of partial classes must have the same accessibility level and those must be created within the same namespace and assembly.
  3. If any partial class is defined as abstract, sealed, or base, the whole class will be considered as the same type.
  4. In c#, a class can have a single base class so the partial classes that we defined for a particular class must inherit from the same single base class type.
  5. If required, we can also create nested partial types.

C# Partial Class Example

Following is the complete example of creating the partial classes in c# by splitting the functionality of a particular class into multiple files using the partial keyword.

 

using System;

namespace Tutlane
{
  //File1.cs
  public partial class Products
  {
     private string Name;
     private int Price;
     public Products(string a, int b)
     {
        this.Name = a;
        this.Price = b;
     }
  }

  //File2.cs
  public partial class Products
  {
     public void GetDetails()
     {
        Console.WriteLine("Name: " + Name);
        Console.WriteLine("Location: " + Price);
     }
  }

  class Program
  {
     static void Main(string[] args)
     {
        Products u = new Products("Mobile", 10000);
        u.GetDetails();
        Console.WriteLine("Press Enter Key to Exit..");
        Console.ReadLine();
     }
  }
}

 If you observe the above partial class example, the functionality of Products class is split across multiple files (File1.cs and File2.cs) using the partial keyword. During the application compilation, both the partial classes will be merged into a single class.

 

When we execute the above example, we will get the result below.

 

C# Partial Class Example Result

 

The main advantage of partial class in c# is to split the functionality of the class into multiple class files. In c#, we can also create partial methods, interfaces, or structures by splitting the functionality of a particular structureinterface, or method into two or more source files using the partial keyword based on our requirements.