Why is Marshal.DestroyStructure considered clean?

Marshal.DestroyStructure is marked with a Pure attribute in the .NET Framework, but I'm not sure why when it explicitly affects the context that calls it.

The state is changed (the pointer is freed), even if it does not directly modify the pointer instance itself.

It is understood that the question is: can a developer honestly note something clean, even if she knows that she indirectly indirectly affects the context state?

+6
source share
1 answer

This is pure in the sense that it does not affect the visible controlled state, which means that for the purpose of code contracts, a method call cannot violate class invariants.

Of course, it is a little misleading to think of the method as pure, as it has side effects, even if these side effects are not visible. They are observable (if you try to use the pointer after it is released, you will fail), but not exactly visible (you cannot say without trying to use the pointer that something is wrong.) I'm not sure what was the motive for marking the Pure method since I cannot understand why this was ever used in the Contracts block, but I suppose there must have been some reason somewhere deep in the BCL contracts that required it.

+2
source

All Articles