What does happen with nullable

public static void Write(this IYamlStream stream, string Key, ref string? value, ScalarStyle style = ScalarStyle.Any) i have this, what does happen when value is non nullable? is it boxed? can i somehow write an extension method for a nullable value and a non nullable value? when doing so it says that the metohd is allready defined but one method is about a boxed string and the other one isnt so isnt it different in the end?
7 Replies
Pobiega
Pobiega9mo ago
strings are reference types and do not get boxed
Joreyk ( IXLLEGACYIXL )
oh... so for primtives like int and int? it makes 2 different methods but not for string as int is a value type
reflectronic
reflectronic9mo ago
yes, you will see that if you try making an overload with ref string instead of ref string? it will not let you it is an annotation for the compiler to provide helpful warnings, it does not change the behavior at runtime in any way (when applied to reference types)
Pobiega
Pobiega9mo ago
int? and int are two different types, so that works fine int? is just syntax sugar for Nullable<int>
Joreyk ( IXLLEGACYIXL )
thank you guys
FusedQyou
FusedQyou9mo ago
Even if you have int? it does not get boxed because c# just wraps it in a struct that imitates a nullable context That's why you always need to do int.value As for combining it with ref, dunno
TheBoxyBear
TheBoxyBear9mo ago
ref doesn't add runtime variability on the type and can't be stored long-term on the heap so no need for boxing. It's a local reference to a struct like any other struct (or primitive)
Want results from more Discord servers?
Add your server