What happens when there is a circular reference between two structs? How does memory allocation work for the example below or would it yield and error?
public struct MultipleChoiceQuestion
{
public IEnumerable<Option> Options;
}
public struct Option
{
public MultipleChoiceQuestion Question;
}
The
Optionsfield inMultipleChoiceQuestionis just a reference here, which will benullby default. TheQuestionfield inOptionis a value of typeMultipleChoiceQuestionwhich will have its default value by default. There’s no problem here (other than a questionable design in more ways than one).A genuine circular reference leading to a logical memory problem will fail to compile: