Abstract, Sealed, Static Classes and Extension Methods in C#

Abstract Members

Abstract members are function members intended to be overridden in derived classes. Key characteristics:

  • Must be a function member: methods, properties, indexers, and events are allowed; fields and constants are not.
  • Declared with the abstract keyword.
  • Contain no implementation—use semicolons instead of method bodies or accessors.
public abstract void DisplayMessage(string message);

public abstract int Value { get; set; }

Important notes:

  1. Abstract members must be overridden in non-abstract derived classes using the override keyword (not virtual).
  2. Like virtual members, overrides require the override modifier.
Virtual Members Abstract Members
Keyword virtual abstract
Implementation Provided None (semicolon only)
Override Requirement Optional Mandatory (unless derived class is also abstract)
Supported Member Types Methods, Properties, Events, Indexers Methods, Properties, Events, Indexers

Abstract Classes

An abstract class serves as a base for inheritance and cannot be instantiated directly.

  • Declared with abstract.
  • May contain both abstract and concrete (implemented) members.
  • Can inherit from another abstract class.
  • Any non-abstract derived class must implement all inherited abstract members.
abstract class Shape
{
    public const int Sides = 3;
    public double EdgeLength = 5.0;

    public abstract double Area { get; }

    public abstract void Render();

    public double Perimeter() => Sides * EdgeLength;
}

class Triangle : Shape
{
    private double _area;
    public override double Area => _area;

    public override void Render()
    {
        Console.WriteLine("Rendering triangle");
    }
}

Sealed Classes

A sealed class cannot be inherited. It is marked with the sealed keyword and is used when further derivation is unnecessary or undesirable.

sealed class FinalProcessor
{
    public void Execute() { /* logic */ }
}

Static Classes

Static classes are used to group utility functions or constants that do not depend on instance data.

  • All members must be static.
  • Cannot be instantiated or inherited (implicitly sealed).
  • May define a static constructor (no access modifiers, runs once before first use).
  • Members accessed via class name (e.g., MathUtils.Sqrt(4)).
  • Supports using static for direct member access.
public static class MathUtils
{
    public static readonly double PI = 3.14159;

    public static bool IsEven(int n) => n % 2 == 0;

    public static int Double(int x) => x * 2;

    static MathUtils()
    {
        Console.WriteLine("MathUtils initialized");
    }
}

static void Main()
{
    Console.WriteLine(MathUtils.Double(3)); // Output: 6
}

Extension Methods

Extension methods allow adding functionality to existing types without modifying their source—useful for sealed or third-party classes.

Requirements:

  • Defined in a static class.
  • The method itself must be static.
  • The first parameter uses the this keyword followed by the type being extended.
public sealed class Point3D
{
    public double X, Y, Z;
    public Point3D(double x, double y, double z) => (X, Y, Z) = (x, y, z);
    public double Magnitude() => Math.Sqrt(X*X + Y*Y + Z*Z);
}

static class PointExtensions
{
    public static double AverageCoordinate(this Point3D p)
    {
        return (p.X + p.Y + p.Z) / 3.0;
    }
}

static void Main()
{
    var point = new Point3D(1, 2, 3);
    Console.WriteLine($"Avg: {point.AverageCoordinate()}"); // Output: Avg: 2
}

Tags: C# abstract class sealed class static class Extension Methods

Posted on Sat, 31 Oct 2026 16:47:32 +0000 by trent2800