Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

7. Properties

Properties are encapsulated fields that allow functions to be ran when their value is read (getter) or modified (setter).

Property declarations consist of optional modifier keywords, it's type, a unique name, getters and setter definitions, and optionally an assignment.

Both of these examples are valid properties:

public i32 MyNumber { get; set; } = 16;
public i32 MyNumber 
{
    // Type.MyNumber will return MyNumber's current value + 10.
    get 
    {  
        return value + 10;
    }

    // Type.MyNumber = 10 will print some text and then update MyNumber's current value.
    set 
    {
        printf("Set MyNumber.");
        field = value;
    }
}