Sitemap
Dot Net, API & SQL Learning

A comprehensive guide to mastering .NET Core, API and SQL, designed to enhance development skills and database expertise.

Do you know the difference between a sealed class and a static class in C#?

4 min readMar 16, 2025

--

Press enter or click to view image in full size
Photo by Christopher Gower on Unsplash

The classes in C# is a blueprint for creating objects but not all of the classes are meant to be instantiated or inherited. That is why the sealed class and static modifiers are used.

Sealed Class

Let’s discuss with the Sealed Class. When you declare a class as sealed, that class cannot be inherited by other classes. On the other hand you can still create instances or objects of the sealed Class.

Why we use Sealed Class -

The main reason to seal a class is to prevent further inheritance. You might want to do that if you have created a class with specific functionality and also for reasons that are related to ensuring consistent behavior, enhancing security or avoiding potential complications.

This is why you don’t want any other class to inherit from your sealed class.

Static Class

The Static Class in C# is a class that cannot be instantiated. Which means that you cannot create objects of a static class. Also, a static class can only contain static members.

Why we use Static Class -

So now the point is, why would we want to use a Static Class. The static Classes are typically used to represent utilities or functionalities that don’t need to be tied to an instance.

Get Code Crack’s stories in your inbox

Join Medium for free to get updates from this writer.

In this way, static classes provide a way to group related methods without the overhead of creating an object. For example, in C#, the Math class is a static class because it serves as a collection of mathematical functions that don’t require an object to be instantiated to access these functions.

Example

Now let’s take a look at some code examples. The first one demonstrates the use of the sealed class.

Example of Sealed Class

using System;

namespace SealedClassExample
{
// Sealed class: prevents other classes from inheriting from it
public sealed class PaymentProcessor
{
// Allowed: Normal properties and methods inside the sealed class
public string PaymentMethod { get; set; }

// Allowed: Constructor in a sealed class
public PaymentProcessor(string paymentMethod)
{
PaymentMethod = paymentMethod;
}

// Allowed: Methods in a sealed class
public void ProcessPayment(decimal amount)
{
Console.WriteLine($"Processing {PaymentMethod} payment of {amount:C}");
}
}

// Not Allowed: Inheriting from a sealed class
// Error: 'SecurePaymentProcessor' cannot derive from
// sealed type 'PaymentProcessor'

/*
public class SecurePaymentProcessor : PaymentProcessor
{
}
*/

// Example usage
class Program
{
static void Main()
{
PaymentProcessor processor = new PaymentProcessor("Credit Card");
processor.ProcessPayment(100.00m);
}
}
}

Key Points:

✅ Below are the points Allowed in Sealed Class:

  • Declaring properties, methods, and constructors inside the sealed class.
  • Creating instances of the sealed class.

❌ Below are the points Not Allowed in Sealed Class:

  • Inheriting from a sealed class.
  • Using the sealed modifier on static classes (they’re implicitly sealed).

Example of Static Class

using System;

namespace StaticClassExample
{
// Static class: cannot be instantiated or inherited
public static class MathHelper
{
// Allowed: Static fields
public static readonly double Pi = 3.14159;

// Allowed: Static methods
public static double Square(double number)
{
return number * number;
}

// Allowed: Static properties
public static string Description { get; set; } = "Provides math utilities";

// Not Allowed: Instance members (fields, properties, methods)
/*
public int Count = 0; // Error: Static classes cannot have instance fields
public void Print() {} // Error: Static classes cannot have instance methods
*/

// Allowed: Static constructor (runs once when the class is accessed)
static MathHelper()
{
Console.WriteLine("MathHelper initialized.");
}
}

// Not Allowed: Inheriting from a static class
/*
public class AdvancedMath : MathHelper
{
}
*/

class Program
{
static void Main()
{
// Allowed: Accessing static members directly through the class name
Console.WriteLine(MathHelper.Description);
Console.WriteLine($"Pi: {MathHelper.Pi}");
Console.WriteLine($"Square of 5: {MathHelper.Square(5)}");

// Not Allowed: Creating an instance of a static class
/*
MathHelper helper = new MathHelper(); // Error: Cannot create an instance of a static class
*/
}
}
}

Key Points:

✅ Below are the points Allowed in Static Class:

  • Declaring static fields, properties, and methods.
  • Defining static constructors.
  • Accessing static members directly via the class name (e.g., MathHelper.Square(5)).

❌ Below are the points Not Allowed in Static Class:

  • Instantiating a static class.
  • Inheriting from a static class.
  • Defining instance members (non-static fields, properties, or methods).

--

--

Code Crack
Code Crack

Written by Code Crack

Passionate software developer with expertise in building scalable web applications. Skilled in C#, .NET Core, Angular, and modern frontend/backend technologies.

Dot Net, API & SQL Learning
Dot Net, API & SQL Learning

Published in Dot Net, API & SQL Learning

A comprehensive guide to mastering .NET Core, API and SQL, designed to enhance development skills and database expertise.