What is meant by the terms managed resource and unmanaged resource in .NET? How do they come into the picture?
C# – meant by “managed” vs “unmanaged” resources in .NET
cmanagednetunmanaged
Related Topic
- C# – Case insensitive ‘Contains(string)’
- C# – How to get a consistent byte representation of strings in C# without manually specifying an encoding
- C# – Proper use of the IDisposable interface
- .net – Difference between decimal, float and double in .NET
- C# – Easiest way to split a string on newlines in .NET
- .net – Difference between “managed” and “unmanaged”
- C# – How to turn a C# object into a JSON string in .NET
- .net – the difference between .NET Core and .NET Standard Class Library project types
Best Answer
The term "unmanaged resource" is usually used to describe something not directly under the control of the garbage collector. For example, if you open a connection to a database server this will use resources on the server (for maintaining the connection) and possibly other non-.net resources on the client machine, if the provider isn't written entirely in managed code.
This is why, for something like a database connection, it's recommended you write your code thusly:
As this ensures that
.Dispose()
is called on the connection object, ensuring that any unmanaged resources are cleaned up.