struct automatically initialized with a value, so in your example, the Unit value contains a Unit value that contains a Unit value that contains a Unit value, etc ...
In the class example, only a reference to null initialized, which does not have infinite regression. However, you ran into a similar problem if you did the following:
class Unit { Unit mUnit = new Unit(); }
Now build the construction of Unit a Unit , which builds a Unit , etc. In this case, you will encounter an overflow of the stack stack at runtime if you try to create an instance of the Unit object.
I thought you could avoid the struct problem by using a type with a null value, but the problem remains. I believe this is because the nullable struct value is still created when set to null .
Dave cousineau
source share