CatSandwich
❔ Benefit of ref structs
Hey folks!
I've been trying for a bit to understand the purpose of using a ref struct over a regular struct. I understand that it can only live on the stack, which is enforced via a set of usage rules, but regular structs could do that too if used properly.
I asked my good old friend chatgpt to help me understand it, and the answer I got was basically that the compiler is able to optimize the usage of ref structs better given its restrictions. Is this accurate? Is it the only benefit?
I'm used to concepts like the
out
keyword where its usage restrictions (must be populated by return, cannot be read) come as a tradeoff for visible benefits (inline variable declaration in a method call), so I guess I was expecting something similar in this case, specifically something that can only be done with ref structs.60 replies
✅ Configure Xml Serialization
Hey folks! I have a type like this:
I'm serializing this via System.Xml. I would like to have this type serialize as if I were serializing the wrapped
Value
, rather than the whole type.
In other words, I want it to serialize as this:
rather than
What are my options to make this happen? I would ideally like to avoid changing the serializer itself, but rather use an approach that the serializer will respect. For example, newtonsoft json uses JsonConverter
, but I'm not sure if System.Xml has a similar concept.18 replies