I'm reading a book which talks about struct value type:
if your override of the virtual method calls into the base type's implementation of the method, then the value type instance does get boxed when calling the base type's implementation so that a reference to a heap object gets passed to the this pointer into the base method.
So below is simple code that demostrates:
internal struct Point
{
private readonly Int32 m_x, m_y;
public Point(Int32 x, Int32 y) {
m_x = x;
m_y = y;
}
//Override ToString method and call base method fromSystem.ValueType
public override string ToString() {
return base.ToString(); //doesn't make sense, just for demo purpose
}
}
class Program
{
static void Main(string[] args) {
Point p1 = new Point(10, 10);
p1.ToString();
}
}
My question is, what does "a reference to a heap object gets passed to the this pointer into the base method" mean? I simply call the base method as base.ToString();
, I didn't pass this
pointer argument in the base method, or how a this
pointer get passed into the base method?
question from:
https://stackoverflow.com/questions/65939540/why-and-how-does-a-value-type-get-boxed-when-you-override-the-virtual-method-cal 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…