I have been learning C# this past summer, and though I have a pretty good understanding of the language, I came to a realization today. How is a statement such as :
Person x = new Person(); any different from a C++ equivalent statement.
What I mean by this is, isnt x essentially a pointer? Because the 'contents' of x are on the heap. How is it any different? Would someone mind clearing this up!
Thanks!
Loading
AlanPosted Aug 6, 2008, 2:58 PM
Yes, in C#, 'x' is essentially a pointer (or reference) to where the Person object is stored on the heap. However, the pointer is not necessarily fixed because of the operation of the garbage collector and therefore needs to be tracked and adjusted if necessary when the GC runs to ensure it still points to the same object.
In contrast, heap objects don't move in (unmanaged) C++ and so the pointer remains the same.