default value for Auto Properties of struct #1083
Unanswered
MkazemAkhgary
asked this question in
General
Replies: 1 comment
-
I've asked before and I'd prefer this: can we do the right thing and make the CLR pattern-fill instead of zero-init for structs that have fields initialized with constant data? You can do this at great speed. |
Beta Was this translation helpful? Give feedback.
0 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.
-
based on this conclusion #1082, having default constructor for structs will make a disagreement between developers whether
default
should have same value asnew S
or not.This proposal is trying to avoid this ambiguity by solving problem in different way.
before we start, I want to mention that
default
will be always zero and default constructor is not allowed. same as before. now here is proposal:allow to set default value for auto properties of an struct.
Auto Properties default (applicable with constants):
Equivalent of:
As you can see in this workaround, we used offset value to achieve practical default value of struct. it doesn't matter if you use
new S
ordefault(S)
, default value will be always what you explicitly set to property.notes:
benefits:
drawbacks:
rules for this section:
Auto Properties default (for Boolean):
for Booleans this is achieved simpler.
Equivalent of:
Additional support for String, Reference types, Generic types (applicable with lazy evaluation):
Similar to singleton pattern, use a flag.
Equivalent of:
notes:
string?
was used for clarity, (valid syntax from Nullable strawman #790)benefits:
drawbacks of this section:
Beta Was this translation helpful? Give feedback.
All reactions