Partial types allow you to split the definition of a class, struct, interface, or method into multiple source files. During compilation, all the parts are combined into a single type.
In this chapter, you will learn what partial types are, how to create them, and how to use partial classes with practical examples.
Partial types are created by using the partial keyword. They allow a single type to be divided into two or more source files that makes large projects easier to organize and maintain. When the application is compiled, the compiler combines all the partial definitions into one complete type.
The partial keyword can be used with:
A partial class is created by declaring the same class with the partial keyword in multiple source files. All parts must have the same class name and belong to the same namespace.
The following syntax is used to create a partial class in C#:
// File1.cs
public partial class ClassName
{
// Members
}
// File2.cs
public partial class ClassName
{
// More members
}
In this syntax:
The following examples demonstrate how to use partial types in C#.
This example demonstrates how to split a class into two source files. One file contains the deposit functionality, while the other contains the withdraw functionality.
File Name: Customer.cs
using System;
namespace CSharpFeatures
{
partial class Customer
{
public void DepositAmount(int depositAmount)
{
amount += depositAmount;
Console.WriteLine(depositAmount + " amount is deposited");
Console.WriteLine("Available balance is: " + amount);
}
}
}
File Name: Customer2.cs
using System;
namespace CSharpFeatures
{
partial class Customer
{
private int amount;
public int Amount
{
get { return amount; }
set { amount = value; }
}
public void Withdraw(int withdrawAmount)
{
amount -= withdrawAmount;
Console.WriteLine(withdrawAmount + " is withdrawn");
Console.WriteLine("Available balance is: " + amount);
}
}
}
File Name: Program.cs
using System;
namespace CSharpFeatures
{
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer();
customer.Amount = 2000;
Console.WriteLine("Current balance is: " + customer.Amount);
customer.DepositAmount(1000);
customer.Withdraw(500);
}
}
}
Output:
Current balance is: 2000 1000 amount is deposited Available balance is: 3000 500 is withdrawn Available balance is: 2500
Explanation
In this example, the Customer class is divided into two source files by using the partial keyword. One file contains the DepositAmount() method, while the other contains the Withdraw() method, property, and field. During compilation, both files are combined into a single Customer class.
This example shows how to split a structure into multiple source files by using the partial keyword.
File Name: Student1.cs
using System;
partial struct Student
{
public int Id;
}
File Name: Student2.cs
using System;
partial struct Student
{
public string Name;
public void Display()
{
Console.WriteLine("ID: " + Id);
Console.WriteLine("Name: " + Name);
}
}
File Name: Program.cs
using System;
class Program
{
static void Main()
{
Student s;
s.Id = 101;
s.Name = "John";
s.Display();
}
}
Output:
ID: 101 Name: John
Explanation
In this example, the Student structure is divided into two files. One file contains the Id field, while the other contains the Name field and the Display() method. The compiler combines both parts into a single structure.
This example demonstrates how to split an interface into multiple source files.
File Name: IEmployee1.cs
using System;
partial interface IEmployee
{
void Display();
}
File Name: IEmployee2.cs
using System;
partial interface IEmployee
{
void Work();
}
File Name: Program.cs
using System;
class Employee : IEmployee
{
public void Display()
{
Console.WriteLine("Employee Details");
}
public void Work()
{
Console.WriteLine("Employee is working.");
}
}
class Program
{
static void Main()
{
Employee emp = new Employee();
emp.Display();
emp.Work();
}
}
Output:
Employee Details Employee is working.
Explanation
In this example, the IEmployee interface is split across two files. One file declares the Display() method, while the other declares the Work() method. During compilation, both interface definitions are merged into a single interface that is implemented by the Employee class.
We request you to subscribe our newsletter for upcoming updates.