Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An abstract class in C# is an incomplete class blueprint declared with the abstract modifier that cannot be directly instantiated. It serves strictly as a base class in an inheritance hierarchy, enforcing a structural contract by defining abstract members that derived classes must implement, while optionally providing shared concrete implementations and state.
public abstract class AbstractBase
{
    // Abstract method: No body, must be overridden in derived class
    public abstract void Execute();

    // Abstract property: Uses accessor blocks, no implementation
    public abstract int Count { get; set; }

    // Concrete method: Has a body, inherited as-is
    public void Initialize()
    {
        // Implementation details
    }
}

Core Technical Mechanics

Instantiation Rules Attempting to instantiate an abstract class using the new operator results in compiler error CS0144. When a concrete derived class is instantiated, memory is allocated for the derived object as a whole, which encompasses the state and fields defined by the abstract base class. The abstract class itself is never allocated memory as a distinct entity. Abstract Members Methods, properties, events, and indexers can be marked as abstract.
  • They are permitted within abstract classes, abstract records, and explicitly within interfaces.
  • They do not provide an implementation body. For methods and events, the declaration ends with a semicolon. For properties and indexers, the declaration uses accessor blocks without bodies (e.g., { get; set; }).
  • They are implicitly virtual.
  • Within abstract classes, they cannot be marked as private, static, or virtual. (Note: C# 11 introduced static abstract members, but this feature is strictly restricted to interfaces).
Implementation Enforcement When a non-abstract (concrete) class inherits from an abstract class, it must provide an implementation for all inherited abstract members using the override keyword. If a derived class does not implement all abstract members, the derived class itself must also be declared abstract. Abstract Override A derived class can override an inherited virtual or override member and mark it as abstract. This removes the default implementation and forces further derived classes in the hierarchy to provide a new implementation.
public abstract class DataProcessor
{
    // State can be maintained
    protected readonly string ConnectionString;

    // Constructors are permitted and called via derived class 'base'
    protected DataProcessor(string connectionString)
    {
        ConnectionString = connectionString;
    }

    public abstract void Process();
    
    public virtual void Log() 
    { 
        // Default implementation
    }
}

public abstract class IntermediateProcessor : DataProcessor
{
    protected IntermediateProcessor(string connectionString) : base(connectionString)
    {
    }

    // Abstract override: Removes default implementation, forcing further derivation
    public abstract override void Log();
}

public class SqlProcessor : IntermediateProcessor
{
    public SqlProcessor(string connectionString) : base(connectionString)
    {
    }

    // Mandatory implementations
    public override void Process()
    {
        // Concrete implementation using inherited state
    }

    public override void Log()
    {
        // Concrete implementation mandated by the abstract override
    }
}

Compiler Constraints and Modifiers

  • Sealed Conflict: An abstract class cannot be marked with the sealed modifier. The abstract keyword mandates inheritance, whereas sealed explicitly prevents it. Applying both modifiers to a class yields compiler error CS0418.
  • Constructors: Abstract classes can define constructors. Because the class cannot be instantiated directly, these constructors are typically marked protected and are invoked during the initialization phase of a derived class via the base() constructor call.
  • Static Members: Abstract classes can contain static fields, properties, and methods. These can be accessed directly via the abstract class type without requiring instantiation.

Abstract vs. Virtual Members

Within an abstract class, developers must distinguish between abstract and virtual modifiers:
  • An abstract member has no implementation and must be overridden by a concrete derived class.
  • A virtual member has a default implementation and may optionally be overridden by a derived class.
Master C# with Deep Grasping Methodology!Learn More