Abstraction and virtual dispatch

virtual, abstract and override let a base struct define behavior that derived structs specialize through dynamic dispatch.

Virtual methods

A virtual method provides a base implementation and reserves a dispatch slot that a derived struct may replace.

struct Entity
{
    public virtual int Score()
    {
        return 0;
    }
}

Overrides

A derived member uses override to replace a compatible inherited virtual or abstract member. Its signature must match the inherited declaration.

struct Enemy : Entity
{
    public override int Score()
    {
        return 42;
    }
}

The explicit modifier keeps replacement intentional: a declaration matching an inherited virtual member must say override.

Dynamic dispatch

Calls through a base reference or pointer select the override associated with the runtime struct.

Enemy enemy = Enemy { };
Entity& entity = enemy;

int score = entity.Score(); // calls Enemy.Score

The compiler maintains stable virtual slots across the inheritance chain so the same base operation resolves to the correct derived implementation.

Abstract members

An abstract member declares required behavior without a body. A struct with an unresolved abstract member is abstract and cannot be instantiated. A derived struct becomes concrete after providing every required override.

struct Entity
{
    public abstract int Score();
}

struct Enemy : Entity
{
    public override int Score()
    {
        return 42;
    }
}

Abstractness follows from unresolved abstract members; it does not require a separate modifier on the struct declaration.

Properties and indexers

Properties and indexers participate in the same virtual model. Abstract accessors end with semicolons, while an override supplies accessor bodies.

struct Source
{
    public abstract int Value { get; }
}

struct FixedSource : Source
{
    public override int Value
    {
        get { return 42; }
    }
}