1How Do These Classes Work?
Hi friends! In this article, we will learn about three important C# concepts: Partial Classes, Sealed Classes and Static Classes.
Each one solves a different programming problem. Partial classes help organise large classes across multiple files. Sealed classes prevent inheritance. Static classes allow us to group reusable methods and properties without creating objects.
We commonly use these concepts in enterprise applications, reusable libraries and application architecture.
2Think About a Banking Application
Imagine we are building a banking application.
Our Customer class contains customer details, contact information and address information. Instead of keeping everything in one file, we can split the same class into multiple files using partial.
Now imagine a PaymentProcessor class containing important payment logic. We do not want another class to inherit and change its behaviour. We can use sealed.
Finally, we need a common utility class to calculate transaction charges. We can create a static class and call its methods directly.
3Understand the Three Class Types
1. Partial Class
The partial keyword allows one class to be declared across multiple files. During compilation, C# combines those declarations into one class. All declarations must use the partial keyword.
2. Sealed Class
A sealed class cannot be inherited by another class. It is useful when we want to prevent further customisation through inheritance.
3. Static Class
A static class cannot be instantiated or inherited. It contains only static members, which we access directly using the class name.
| Feature | Partial | Sealed | Static |
|---|---|---|---|
| Purpose | Split class | Stop inheritance | Shared functionality |
| Inheritance | Allowed | Not allowed | Not allowed |
| Object creation | Allowed | Allowed | Not allowed |
| Multiple files | Yes | Not required | Not required |
4Build a Working C# Application
Let's implement all three concepts in one application. The partial Customer class is split into two files. The other classes demonstrate sealed and static behaviour.
public partial class Customer
{
public string Name { get; set; }
}
public partial class Customer
{
public string City { get; set; }
}
using System;
public sealed class PaymentProcessor
{
public void Process()
{
Console.WriteLine("Payment processed");
}
}
public static class BankUtility
{
public static double CalculateCharge(double amount)
{
return amount * 0.02;
}
}
class Program
{
static void Main()
{
Customer customer = new Customer
{
Name = "Rahul",
City = "Bangalore"
};
Console.WriteLine(customer.Name);
Console.WriteLine(customer.City);
new PaymentProcessor().Process();
double charge =
BankUtility.CalculateCharge(5000);
Console.WriteLine("Charge: " + charge);
}
}
Notice that Customer is declared in two files. Both declarations combine into one class. PaymentProcessor is sealed, and BankUtility is called without creating an object.
5See What the Application Prints
Rahul Bangalore Payment processed Charge: 100
The customer details come from the combined partial class. The payment processor executes its method, and the static utility calculates 2% of ₹5,000, which is 100.
6Follow the Code in Visual Studio Code
Watch how the application uses the three class types during execution.
7Choose the Right Class Type
These three keywords solve different problems in C#. Partial helps maintain large classes, sealed protects class inheritance, and static provides shared functionality.
- Use partial when a class needs multiple files.
- Use sealed when inheritance should be restricted.
- Use static for functionality that does not require object instances.
8Questions Interviewers Love to Ask
What is a partial class in C#?
Answer: A partial class allows us to split one class declaration across multiple files. The compiler combines them into one class.
Can we inherit a sealed class?
Answer: No. A sealed class cannot be used as a base class for another class.
Can we create an object of a static class?
Answer: No. Static classes cannot be instantiated. We access their members directly using the class name.
Can a partial class have different access modifiers in different files?
Answer: No. All declarations of the same partial class must use consistent accessibility and compatible class modifiers.
Can a sealed class contain virtual methods?
Answer: No. A sealed class cannot declare new virtual methods because no derived class can override them. It can override inherited virtual methods.