CLI is a managed
platform. It means that you don't directly allocate, but, more importantly, you do not control reclaiming memory. In terms of C#, you always allocate managed heap memory with "new" operators and never need to clean up the memory. It is done through the mechanism of Garbage Collection
. Please see:
You should also understand that the managed pointers (memory handles, known in C# as references) are complex objects very different from unmanaged pointers. The objects allocated in memory could be automatically moved during run time to by the platform, to optimize memory layout, avoid fragmentation, etc. Any program assuming fixed location of any object in linear memory
(used, for example, in Windows architecture) is incorrect. This is not usually a problem, because most code use memory only through handles.
See also: http://en.wikipedia.org/wiki/Flat_memory_model
The advanced feature is using pointers
in managed memory, which only possible in an
statement, but pointer operations are done through the special process of pinning
of a fragment of memory. In C#, this is done via the
statement. Please see:
Another advanced feature is using the Garbage Collector (CG) API. Please see:
I strongly discourage doing it.
Garbage collection is very hard to improve controlling it manually; you can only make things worse.
Finally, it's possible to use unmanaged memory in a CLI application indirectly, for example, via P/Invoke. This goes beyond the topic of the question, but the idea is: this is done independently by the unmanaged code itself; your managed code only calls appropriate methods. As a matter or rule, reclaiming of unmanaged resource is done via
, but this interface is used not only for this purpose. In C#, it's important to use
statement whenever possible. Please see:
I would advice to read at least basic introductory documentation before trying to do programming, to avoid total frustration and troubles.