C
C#12mo ago
Tim

Readonly struct property vs member variable

What would make the difference between
readonly struct Values{
float MyValue { get; }
// .. constructor
}
readonly struct Values{
float MyValue { get; }
// .. constructor
}
and
readonly struct Values{
readonly float MyValue;
// .. constructor
}
readonly struct Values{
readonly float MyValue;
// .. constructor
}
I see the approach using the property lose it's value here, since the struct is already readonly (e.g. copying is useless) Is there a 'better' method between these two? Or better said, why would anyone even use the first approach?
2 Replies
TheRanger
TheRanger12mo ago
No Difference really except when you use reflection on them, afaik and the latter one is faster to access since ur accessing it directly instead of accessing the backing field through the getter
jcotton42
jcotton4212mo ago
Something something pretty sure the JIT reliably inlines autoprops
Want results from more Discord servers?
Add your server