Values declared with val
cannot be reassigned.
A val
can be declared anywhere in Kotlin so there’s no direct equivalent in C#. Nearest equivalents are:
readonly
fieldsinit
properties in C# 9.0Kotlin
val firstName: String = "Bobby"
val lastName = "McBobface" // Type can be inferred
lastName = "Smith" // ERROR! Cannot reassign
A val
can be declared and initialised separately (a bit like a readonly
in C#), but then the type can’t be inferred.
val firstName: String
// Later
firstName = "Bobby"