❔ static fields for struct types
(1)
or as Utility class
(2)
The first and second one are the same but I have seen some libs prefer to implement them like (2)
(3)
and what is the benefit of implementation (3)
4 Replies
What about defensive copy for readonly structs?
The arrow syntax defines a property. It's almost always preferred to use properties instead of public fields so you can change your implementation later (for instance, add some input validation on set) without it being a breaking change to consumers of your class.
In general, don't expose public fields.
I'm not sure what you mean by defensive copy of readonly structs. Any read of a struct is generally going to make a copy.
The benefit of 3, for simple struct creations, is that you're not taking up space for an unnecessary field
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.