The readonly keyword is used to create fields that are read-only from any place outside the constructor (or class constructor). These fields therefore are immutable and are useful when writing thread-safe classes. Instead of changing the values of the fields on immutable classes, new classes are created with the new values.
The "readonly" directive
type MyClass = class var i: Integer; readonly; ... end;
type ImmutableClass = public readonly class public property Name: string; property Address: string; constructor(aName, anAddress: string); end; constructor ImmutableClass(aName, anAddress: string); begin Name := aName; Address := anAddress; end;
Adding the readonly keyword to a class makes all fields and properties with implicit fields read-only. They can only be set from the constructor. This feature aids in writing immutable classes.