In Kotlin, you declare:
class
fun
var
or val
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:
get()
/set()
methodsString
properties have to be initialised here. We’ll see soon how to intialise them via the constructor…