Does unboxing occur when a class's value-type member is referenced?
.net, c#, unboxing
Solution
Stop thinking about stack and heap; that's completely the wrong way to think about it. It is emphatically not the case that "boxed" means "on the heap", and therefore anything "on the heap" must be "boxed".
Stack and heap are irrelevant. Rather, think about references and values. A value of value type is boxed when it must be treated as a reference to an object. If you need to have a reference to a value of a value type, you make a box, put the value in the box, and make a reference to the box. And there, now you have a reference to a value of value type.
Do not confuse that with making a reference to a variable of value type; that is completely different. A variable and a value are two very different things; to make a reference to a variable you use the "ref" keyword.
Problem
I read What is boxing and unboxing and what are the trade offs? but can't understand one thing. Suppose I have a class: ``` class MyClass { public int Value { get; set; } } ``` And I want to get value within my method: ``` void MyFunc(MyClass cls) { int i = cls.Value; } ``` As a class placed in heap, I guess that Value placed in a heap too? And therefore operation ``` int i = cls.Value; ``` is unboxing? Or it's not unboxing?