2022/09/01/beware-of-records-with-expressions-and-calculated-properties/ #92
Replies: 1 comment 1 reply
-
Nice wrap up, too bad i found this the hard way, after debugging for several hours. Another problem is that we break equality with any of these approaches: automatically implemented Equals and GetHashCode use all fields and properties, even private ones. So, while we can have a private field for Lazy or for a raw cached value which is initialized once, all that it changes Equals and GetHashCode, breaking record value-semantics and read-onliness. The obvious way around that is to implement Equals and GethashCode yourself, excluding private fields used for caching computed values, which is even worse... After screwing around, i just went with a get-only auto computed property because perf was not as important as readability. |
Beta Was this translation helpful? Give feedback.
-
2022/09/01/beware-of-records-with-expressions-and-calculated-properties/
I’ve been using C# records a lot since the feature was introduced. However, when using them, we really need to understand how they work, otherwise we might face unexpected surprises.
https://blog.codingmilitia.com/2022/09/01/beware-of-records-with-expressions-and-calculated-properties/
Beta Was this translation helpful? Give feedback.
All reactions