C# readonly what is that for? The readonly keyword in C# is used as a modifier. A field can be declared with readonly modifier, and its assignment can only be in the Constructor or when the variable is declared. An example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public readonly int MinAge = 10; //this is valid public class Person { public readonly int _age; public Person(int age) { _age = age; //this is also valid } public void SetAge(int age) { _age = age; //this is invalid, one cannot set it here would give compile error } public int Age { get { return _age; } //this is also valid no setter just getter } } |
The main thing to understand about readonly modifiers is that they are initialized […]