but the value of such an int will still be stored on the stack
This is not necessarily the case. Even when it’s true, it’s a pure detail of the implementation, not part of the language specification. The main problem is that the type system does not necessarily correlate with the storage engine used by the runtime.
There are many cases where calling new in a structure still causes the object to not be on the stack. Boxing is a good example - when you place an object, you basically click on the object (effectively "copying" it into the heap) and referring to the object. Also, anytime you close a value type with a lambda, you end up "allocating a bunch."
If I say that I would not focus on this at all, the problem really should not concern the stack and heap in the distribution, but rather about the types of values and the semantics of the reference type. Therefore, I highly recommend reading Eric Lippert's “Truth About Value Types” and “John Skeet” References and Values . Both of these articles focus on important aspects of struct vs. semantics. class instead of having to view the repository.
Regarding ways to force int to be stored on the heap, here are a few simple ones:
object one = 1; // Boxing int two = 2; // Gets closed over, so ends up "on the heap" Action closeOverTwo = () => { Console.WriteLine(two); } // Do stuff with two here... var three = new { Three = 3 }; // Wrap in a value type...
source share