Please consider implementing "final" variables/properties/fields. #4591
Unanswered
merklegroot
asked this question in
Language Ideas
Replies: 1 comment 2 replies
-
Fields are already covered, the keyword is For |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Init-only setters was a wonderful addition in C# 9.
Please consider taking this a step further by adding "final" variables/properties/fields where the value can only be set in the declaration.
Comparison to other languages
This would match the behavior of the "final" modifier in Java, the "const" modifier in JavaScript/TypeScript, and the default behavior in F# without any modifiers.
(Note that C#'s "const" and JavaScript/TypeScript's "const" have significant differences.)
Example
Comparison to the "const" modifier
The "const" modifier also enforces that the value only be set in the declaration.
However, "final" would not be restricted to compile time constants.
Comparison to init-only properties and readonly fields
Init-only properties and readonly fields restrict that the values can only be set during construction.
I'm proposing that "final" variables (or properties, fields) restrict the values to being set in their declaration.
Thanks for reading!
Beta Was this translation helpful? Give feedback.
All reactions