LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Partial Classes, Sealed Classes and Static Classes

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.

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.

Remember Partial organises code, sealed restricts inheritance, and static provides class-level functionality.

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
Pro Tip Partial classes are especially useful in code generated by tools, such as Windows Forms and Entity Framework scaffolding.
Common Mistake A sealed class can still create objects. Only inheritance is restricted. A static class cannot create objects.

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.

Customer.Basic.cs
public partial class Customer
{
    public string Name { get; set; }
}
Customer.Contact.cs
public partial class Customer
{
    public string City { get; set; }
}
Program.cs
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

Output
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.

Program.cs - Visual Studio Code
● Program.cs   C#
◫⌕⑂⚙
1public partial class Customer
2{
3 public string Name { get; set; }
4}
5public sealed class PaymentProcessor
6{
7 public void Process()
8 {
9 Console.WriteLine("Payment processed");
10 }
11}
12public static class BankUtility
13{
14 public static double CalculateCharge(double amount)
15 {
16 return amount * 0.02;
17 }
18}
PROBLEMSOUTPUTDEBUG CONSOLETERMINAL
PS C:\Projects\Demo> dotnet run
Starting application...
C#UTF-8   CRLF   C#

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

Q1Easy

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.

Q2Easy

Can we inherit a sealed class?

Answer: No. A sealed class cannot be used as a base class for another class.

Q3Medium

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.

Q4Medium

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.

Q5Hard

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.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.