OutParameterUndefinedWhenAttribute(bool value) #8530
Unanswered
TactiTac0z
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
You can use |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It's pretty common in C# that we use / write methods similar to
bool TryGetValue(out T value)
Like for dictionaries, if the return value is false the value gets assigned to default or null.
Depending on inlineability of a method such as that, the runtime may or may not inline the default assignment away atleast if a local variable is used as the parameter.
It would be quite convenient even just at a language level, if we could enforce the consumer via static analysis that an argument supplied to such a method is only definitively assigned if the method return true (or false), but also for a little bit of performance improvements.
MemberNotNullWhen technically does this at a language level, although that wouldn't work for value types where a performance improvement would actually matter.
Draw backs
To be fair there is a quite a bit of draw backs here.
I admit this might be better suited using analyzers and unsafe code when I really want those few clock cycles.
I'm gonna post it anyway in case it can be used for something anyway.
These issues could theoretically be fixed with actual runtime support.
Languages and version that don't understand this new attribute might try to dereference an uninitialized memory block, although to be fair initLocals is emitted by default in pretty much all compilers but still.
If an interior pointer is provided as the argument an actual observable behavior is caused if a consumer code depend on that field or array being set to default. So it would likely not be safe to change bcl methods to use this.
Beta Was this translation helpful? Give feedback.
All reactions