Kotlin for C# Developers

1.2. val

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:

Kotlin

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"

Next: const val