Hi I've often wondered if the following scenario actually happens in c#
If I have a struct but I don't explicitly override any of the methods that derived from object such as ToString(), GetHashCode(), etc then if I declare a local instance of my struct class and call 'ToString()' on it, would my struct get boxed i.e would the CLR convert it implicitly to an object on the heap and then call ToString()? Or is it clever enough to know that there's no implementation for that struct and ignore it?
i.e
public struct Vector2D
{
public float m_x;
public float m_y;
...... etc
}
void SomeFunc()
{
Vector2D aVec = new Vector2D();
Console.WriteLine(aVec.ToString()); // <-- does aVec get boxed here?
.....
}
== Edit - Update== Mehrdad's link to MSDN, whilst being useful has confused me slighly. I'll quote and see if any one can unpick this for me
When a callvirt method instruction has been prefixed by constrained thisType, the instruction is executed as follows:
If thisType is a reference type (as opposed to a value type) then ptr is dereferenced and passed as the 'this' pointer to the callvirt of method.
If thisType is a value type and thisType implements method then ptr is passed unmodified as the 'this' pointer to a call method instruction, for the implementation of method by thisType.
If thisType is a value type and thisType does not implement method then ptr is dereferenced, boxed, and passed as the 'this' pointer to the callvirt method instruction.
So does that mean that if I don't explicitly implement ToString() on my struct type that it will fall into the last case and get boxed? Or am I mis-understanding it somewhere?