Kotlin for C# Developers

3.1. Classes: the basics

In Kotlin, you declare:

C#

public class Thing
{
    private string _readWriteField;

    public string ReadOnlyProperty { get; }

    public string ReadWriteProperty1 { get; set; }

    public string ReadWriteProperty2
    {
        get => LoadValue();
        set => SaveValue(value);
    }
}

Kotlin

class Thing {
    private var readWriteField: String = ""

    val readOnlyProperty: String = ""   // Use val for { get; } behaviour

    var readWriteProperty1: String = ""   // Use var for { get; set; } behaviour

    var readWriteProperty2: String
        get() = loadValue()
        set(value) {
            saveValue(value)
        }
}

Two things to note:

Next: Constructors (part 1)