I'm new to StackOverflow so sorry if anything about this post is weird or poorly formatted, giving it my best here, I'm also infamously bad with programming linguo so I might sound incredibly dumb as I say all this.
Anyway, onto my question: My understanding of struct
has always been that because of the fact that any and every struct
is a value type, the members that the struct
has cached also all need to be value types so that the struct
's byte size can be deterministic and consistent across all "instances" of the struct
(although its not really right to call them instances given they're value types but I'm not sure what other term would be used). To my current understanding, that would strike out any and all reference types from ever being members in struct
s even though it is permitted by my compiler and presumably all C# compilers. That's fine, that's what struct
s are for in my eyes, just encapsulating some related value types and reducing the performance impact of otherwise encapsulating them into a class
that would require instantiation, not to mention not having to deep-copy any struct
"instances" to get a second unique instance to alter. The trouble for me comes along with nullable value types. Sure, they're still value types and so still have a place in any struct
, but the extra byte required by all nullable types to determine if the member is currently null
or not has me wondering if it is right to allow struct
s to cache nullable value types as members? Will I run into any lower-level issues by having, for example, an int
as a member inside of a struct
acting as some access index that has the possibility to be null
?
For examples sake, if I were to write a line such as structB = structA
with a nullable value member as part of the definition of that struct
type, would the member be a pointer to the same nullable object
in memory much as it would if I were doing this with a struct
containing a reference type?
Many thanks for your time spent reading this!