# Final variables in C#

CsharpProgrammingServer Side Programming

Java has a final keyword, but C# does not have its implementation. Use the sealed or readonly keyword in C# for the same implementation.

The readonly would allow the variables to be assigned a value only once. A field marked "read-only", can only be set once during the construction of an object. It cannot be changed.

## Example

class Employee {

Employee(int age) {
this.age = age;
}

void ChangeAge() {
//age = 27; // Compile error
}
}

Above, we have set the age field as readonly, which once assigned cannot be changed.

Published on 14-Aug-2018 12:13:01