According to http://msdn.microsoft.com/en-us/library/ms229017.aspx, value types "are allocated on the stack or inline with other structures*". Yet in the stack is an implementation detail, Eric Lippert states that that's an implementation detail.
To my understanding, an implementation detail is "a behavior produced by code which may be relied on by consuming code, though that behavior is not specified by the spec the code is written to.". I understand that documentation is not a specification, though presumably if something is listed in the documentation that we can rely on it and it is no longer an implmentation detail. So, is the stack an implementation detail or isn't it?
*: I understand this to mean that structs can also be allocated inside of other ones instead of directly on the heap, though I could be wrong.
The MSDN documentation is telling you about the particular implementation that the Microsoft C# compiler uses for
struct
. Those particular details are not in the ECMA 334 C# specification; they are not part of the semantics ofstruct
s. Therefore, those details in the documentation are implementation details.I seem to remember reading somewhere Eric Lippert saying that he wishes (or prefers, I don't remember what level of preference he provided) the documentation made no mention of the stack in connection with
struct
s. I'll see if I can dig it up.Here it is, from the blog post that you linked to:
The relevant section of the ECMA 334 C# specification is §11. Note that the word "stack" is never used in this section. The section merely spells out the syntax, that
struct
s follow value semantics, that they are implicitly sealed and inherit fromSystem.ValueType
, that assignment to a variable ofstruct
type creates a copy, that passing astruct
as a parameter by value creates a copy, howstruct
s are set to default values (all value type fields in thestruct
are set to their default values, and all reference type fields are set tonull
), the rules surrounding boxing and unboxing ofstruct
, the meaning ofthis
forstruct
s, and how field initialization, constructors, destructors and static constructors work forstruct
s. Again, no mention of stacks. Ever.The stack is an implementation detail, not part of the semantics of
struct
.For C# value types the 'stack' is an implementation detail because every safe operation performed on the value type will behave the same way, orthogonal to whether the struct was allocated on stack or on heap.
Operations for which it would make a difference (ie. end up in referencing a deallocated stack frame), like taking the address and using it directly (eg. via legacy APIs), are unsafe and erroneous usage (ie. not using a Marshaling API).