Managed Class vs Unmanaged Class in C# #134
-
Hello, Page 344 ❘ CHAPTER 13 Managed and Unmanaged Memory, below WORKING WITH UNMANAGED RESOURCES, you wrote:
Did you explain what is manage and unmanaged class? and What are the differences between these? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
A better name would be a managed type and an unmanaged type. A managed type is managed by the garbage collector - the garbage collector frees memory, but also can change the address of an object (it moves objects during garbage collect runs and thus can compact memory). An unmanaged type is not managed by the garbage collector. One example is a Another example for an unmanaged type is important with platform interop. Instead of allocating memory on the managed heap (the managed heap is used with "normal" .NET ref types), memory can be allocated on the native heap. The native heap is not managed by the garbage collector, and you need to free the allocated memory yourself. This is important when you invoke native APIs of the platform. How you can do this is shown on page 368, creating a .NET class that invokes different methods on Windows and Linux. Many of the .NET APIs need to invoke native APIs behind the scenes. |
Beta Was this translation helpful? Give feedback.
A better name would be a managed type and an unmanaged type. A managed type is managed by the garbage collector - the garbage collector frees memory, but also can change the address of an object (it moves objects during garbage collect runs and thus can compact memory). An unmanaged type is not managed by the garbage collector.
One example is a
struct
type which is stored on the stack. Be aware that astruct
type can also be stored on the heap which happens with boxing. Aref struct
type can never be stored on the heap, this type only works when it's on the stack.Another example for an unmanaged type is important with platform interop. Instead of allocating memory on the managed heap (the …