Sealed, Static, and Partial Classes in C#
Learn the purpose, differences, and use cases of sealed, static, and partial classes in C# with practical examples.
In C#, classes can be defined with special keywords for different purposes.
sealed classes cannot be inherited, static classes are used only for common (utility) functions,
and partial classes allow splitting the definition of the same class across multiple files.
These features are important for improving code safety, enabling reusability, and supporting teamwork in large projects.
Sealed Class
A class marked with the sealed keyword cannot be inherited by other classes.
This is useful when we don’t want the behavior of a class to be changed.
For example, classes that are security-critical or contain finalized logic can be declared as sealed.
public sealed class PaymentProcessor
{
public void Process()
{
Console.WriteLine("Payment processed.");
}
}
// The following code will give a compile error:
// public class CustomProcessor : PaymentProcessor { }
Static Class
static classes contain only static members and cannot be instantiated.
They are ideal for collecting common utility methods.
For example, they are useful for mathematical operations, date calculations, or formatting functions.
The Math class is an example of this kind. In practice, a static class behaves as if there is only one shared instance across the application.
public static class MathHelper
{
public static int Square(int x) => x * x;
public static int Cube(int x) => x * x * x;
}
class Program
{
static void Main()
{
Console.WriteLine(MathHelper.Square(5)); // 25
Console.WriteLine(MathHelper.Cube(3)); // 27
}
}
Partial Class
partial classes allow you to split the definition of a single class into multiple files.
This is particularly useful in large projects or when working with auto-generated code (e.g., WinForms, WPF),
enabling developers to work on different parts of the same class independently.
// File1.cs
public partial class Person
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
}
// File2.cs
public partial class Person
{
public void Print()
{
Console.WriteLine($"{FirstName} {LastName}");
}
}
class Program
{
static void Main()
{
var p = new Person { FirstName = "John", LastName = "Smith" };
p.Print(); // John Smith
}
}
When to Use?
- Sealed: When you don’t want other classes to inherit from it, ensuring the behavior remains unchanged.
- Static: To collect common, object-independent helper methods.
- Partial: To split large classes or to separate auto-generated code from manual code.
TL;DR
sealed: Class cannot be inherited.static: Cannot be instantiated, contains only static members.partial: A single class can be split into multiple files.
Example Application: Using Static and Partial Classes
In this example, two special class types are used together:
- Static class (Calculator) → Contains common calculation methods without creating an instance.
- Partial class (Product) → Properties and methods are separated into different files.
Calculator.cs
// Static class: contains only static methods, cannot be instantiated with new
public static class Calculator
{
// Applies discount (reduces price by percentage)
public static decimal ApplyDiscount(decimal price, decimal rate)
=> price - (price * rate);
// Adds tax (increases price by percentage)
public static decimal AddTax(decimal price, decimal taxRate)
=> price + (price * taxRate);
}
Product.Part1.cs
// First part of the partial class: Properties are defined here
public partial class Product
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
Product.Part2.cs
// Second part of the partial class: Methods are defined here
public partial class Product
{
// Prints the product
public void Print()
{
Console.WriteLine($"{Name} - {Price:0.00} USD");
}
// Applies discount first, then tax
public void ApplyDiscountAndTax(decimal discountRate, decimal taxRate)
{
// Using Calculator (static class)
Price = Calculator.ApplyDiscount(Price, discountRate);
Price = Calculator.AddTax(Price, taxRate);
}
}
Program.cs
class Program
{
static void Main()
{
// Create a new product object
var p = new Product { Name = "Laptop", Price = 2000m };
Console.WriteLine("Initial price:");
p.Print(); // prints 2000 USD
// Apply 10% discount and 20% tax
p.ApplyDiscountAndTax(0.10m, 0.20m);
Console.WriteLine("Price after discount and tax:");
p.Print(); // prints the price after discount and tax
}
}
Sample Output:
Initial price:
Laptop - 2000.00 USD
Price after discount and tax:
Laptop - 2160.00 USD