This might be a pretty straightforward question, but I’m trying to understand some of the internal workings of the compilation.
Very simply put, imagine an arbitrary object being instantiated. This object is then allocated on the heap. The object has a property of type PointF (which is value type), with a get and a set method.
Imagine the get and the set method containing a few calculations for doing their work. How and where (stack/heap) and when is this code instantiated?
This is the background for this question:
I’m writing get and set methods for an object and these methods need to be accessed very frequently. The get and set code in itself is rather massive so I feared that in a worst case scenario the methods would be instantiated as an object or a value type with all internal code for every access of the property. On the other hand the code is probably instantiated when the main object is created and the CPU is simply told to jmp to the property code start. Anyway, this is what I want to have clarified.
Methods (or properties) do not get instantiated. You seem to be thinking the way that I once did – that creating an instance of a class not only allocates space for the data, but also for the code. This is not true.
Even in C++, the way this would work is that the data would be allocated, along with a pointer to an array of function pointers – the Virtual Function Table or vtable. The entries in the vtable would point to virtual methods. Non-virtual methods would not need an entry in the vtable.
In either case, there was only a single copy of the code, regardless of how many objects were instantiated.