Wouldn't creating a instance of the struct inside the struct create a infinite loop?
Indeed, and if you remove the static keyword, you actually get the following error message:
Struct member 'position.Zero' of type 'position' causes a cycle in the struct layout
So, why does static "fix" the problem? Because static members are not stored per instance of the struct. Instead, static members are something akin to a "global variable" in C#.
Thus, in your example,
- a position is defined by its
x, y, etc. values. These are the values that "take up space" when you declare a new instance of your struct.
- In addition, there is a special "constant"
postition.zero, containing a position with all its values set to the default value. (I put "constant" in quotes, since it's technically not a C# constant, but it behaves similarly to one, since it's readonly.)
static. Leave that out and yes, it would be a problem, and the compiler would complain. But astaticis, storage-wise, part of the class, not the instance.int.MaxValueorDateTime.Now. They're both members of structs and they both have the same type as the struct. But neither of them will cause a cycle because one is a constant and the other is a static member and they're both accessed via the type name itself, not via an instance.