Finished Defining readonly Members; Finished Defining const Members

Jason Zhu 2021-02-22 15:00:23 +11:00
parent 57944415e6
commit fb25385807

@ -48,4 +48,19 @@ Reference:
Instead of implement property, it can be implemented automatically if it's standard getter & (or private) setter (as read-only object). Besides, we can declare setter to be private
Overall, property provide more concise and convenient way to define than field.
Overall, property provide more concise and convenient way to define than field.
## Defining readonly & const Members
References:
* [readonly (C# Reference)](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly)
* [Constants (C# Programming Guide)](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants )
Some class members are decalred to be readonly. There are multiple methods can achieve this objective:
* `readonly` keyword can be used to create readonly field, that can only be initialized during declaration and overwritten in constructor.
* `const` is more strict, it can only be initialized in during declaration, and cannot be overwritten in any place. (It must be defined before compilation).
* Constant fileds are NOT variable.
* Local variable within block can be constant.
* `const` value can be assigned with `public` access modifier, as it cannot be modified
* Normally `const` values are CAPITAL.
* `const` value is treaded as `static` (i.e. belong to class, instead of object as it cannot be changed)