I understand why structs can't contain circular references which lead to logical memory problems, but why doesn't a nullable reference circumvent this limitation? For example:
struct Foo
{
Foo? bar;
}
Obviously this could very easily lead to stack overflows and circular references, if one wasn't careful, but shouldn't bar be a pointer to another Foo instance, and default to null? Or (more likely) do I not understand how nullable value types are laid out in memory?
(My background knowledge consists mainly of information from this question and answers.)
No, not quite. A nullable value type is really an instance of Nullable<> with a value type as the generic parameter. The question mark is just a shorthand.
Nullable is a struct, and therefore is a value type. Since it retains a reference to the Foo struct, you still have a circular reference consisting of value types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With